Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I get current coordinates of rectangle corners? #51

Open
roman-biqmind opened this issue Jun 18, 2019 · 4 comments
Open

How can I get current coordinates of rectangle corners? #51

roman-biqmind opened this issue Jun 18, 2019 · 4 comments

Comments

@roman-biqmind
Copy link

When I try using the events (e.g. rectangle.on('transform', (e) => { ... } )), how can I get live coordinates of my rectangle as I scale/rotate it?

When I move, I can access them via rectangle.getLatLngs(), but in event of rotate or scale the coordinates remain the same until I start the next zoom/rotate.

I need this in order to properly rotate an imageOverlay linked to the rectangle.

@w8r
Copy link
Owner

w8r commented Jun 18, 2019

Take a look at what I suggested in #29
You have access to the transformation matrix

@roman-biqmind
Copy link
Author

@w8r
I see the matrix field on the event, but could you please advise on how to use it? If we have the previous coordinates (rectangle.getLatLngs()) and the transformation matrix, we should be able to calculate new values, correct?

@pedhenrique
Copy link

I see the same problem. Is the intent behaviour to not show the current coordinates on getLatLngs() while rotating or scaling?

@kbrose
Copy link

kbrose commented May 19, 2021

I also had to do this. This is what I came up with:

// Fires on rotate/resize, not on drag.
poly.on("transform", function (e) {
  const trans = poly.transform;
  const coords = poly.getLatLngs()[0]; // pre-transform coordinates of polygon
  const matrix = trans._matrix;
  const map = poly._map;
  function transform(coord) {
    // the key is to project your lat/long coordinates to the layer's
    // coordinate system, apply the transform, and then invert back
    // to the lat/long coordinates
    return map.layerPointToLatLng(
      matrix.transform(map.latLngToLayerPoint(coord))
    );
  }
  // I used ImageOverlay.Rotated from
  // https://github.com/IvanSanchez/Leaflet.ImageOverlay.Rotated
  // which gives a "reposition" function. But you can use transform(coords[i])
  // generally to do your transformation.
  overlay.reposition(
    transform(coords[1]),
    transform(coords[2]),
    transform(coords[0])
  );
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants