-
Notifications
You must be signed in to change notification settings - Fork 1.3k
cameraForLatLngs fits to untilted bounds while in perspective view #2259
Comments
To recap: |
This issue tracks the work to make
The result is that the map is zoomed much farther out than it should be: |
There are a couple possible behaviours in some cases. Imagine a box that's a horizontal line. It has a large width and small height. You could:
Which is better? I prefer the second. |
Absent tilt, you can think of the current behavior as a) centering the shape, then b) zooming to fit (but not panning or rotating). So I’d prefer (2) as well. In the future, we could choose an optimal rotation and pitch for a given object, similar to what the Bing Maps API does, but that would have to be a separate method. |
On iOS, when the map is tilted and you enter targeted course tracking mode (in which both the user location and a target coordinate are fitted to opposite ends of the map view), |
Confirmed an issue on Android as well. When the map is tilted and you try fitting the camera within a bounds, it doesn't take into account the tilt. LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(new LatLng(36.532128, -93.489121)) // Northeast
.include(new LatLng(25.837058, -106.646234)) // Southwest
.build();
mapboxMap.easeCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 50), 5000); Full code can be found in the demo app. This gif is suppose to fit the entire state of Texas within view but cuts it off only when the map is tilted before the animate camera is called: |
following this issue for: CameraUpdateFactory.newLatLngBounds() zooming to greatest possible zoom |
We were facing a similar challenge; this is: How to transform the camera such that the bounds are centered on screen at the greatest possible zoom level while the map is tilted. I'm gonna share the solution I came up with in case is of any use to anyone. My example is using google maps (sorry!) and implemented on iOS but I think it might be useful regardless. Warning: For simplicity I'm assuming the map bearing is 0 (facing north), solving it for other bearings would just mean to add one more rotation matrix on y Please let me know if there is a simpler solution I didn't see :) EDIT: Fixed the math to use meters per pixel instead of calculate the % which leads to an almost pixel perfect implementation (see the preview) and moved the implementation to an example project. I also added the math for calculating the height fit cases PreviewImplementation (example)ExplanationThe way I approached this problem is by transformation matrices. Similar to what So the transformation needed would be: (Translation x Scale x Rotation x Perspective); where: ... One more thing before moving on: because the rotation point needs to be in the center, we need to translate the matrix before transforming it (and after), so the resulting system would be: ... where Next in order to get the projection of a point from the non-tilted rect to the tilted rect we use a function Now the base equations for solving To fit to the width
.. where:
Now if we solve the system of equations with all this information we obtain: To fit to the height
... where These four equations is all the math we need to calculate the translation and the scale values. With this we can apply the scale to the bounds distance and calculate the zoom and offset the center using |
see #13007. This may work for many of these cases. |
I don’t think #13007 addresses this issue at all. |
This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions. |
cc @1ec5
The text was updated successfully, but these errors were encountered: