You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
cameraForLatLngs fits to untilted bounds while in perspective view #2259
To recap: fitBounds does not account for the trapezoidal shape of the bounds when the map is tilted. As a result, the map’s camera fails to round-trip correctly (tending to zoom out) when the map starts out tilted.
This issue tracks the work to make Map::cameraForLatLngs() visually fit to the passed-in bounds when the map is tilted. To illustrate the problem, I rigged iosapp to:
Start out unrotated and untilted, and note the map view’s visibleCoordinateBounds
Add a polygon annotation that covers those bounds
Tilt the map by 60° (by taking the current camera, adjusting its pitch, and setting camera to that modified camera)
Pass the bounds from (1) into -[MGLMapView setVisibleCoordinateBounds:animated:]
The result is that the map is zoomed much farther out than it should be:
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), cameraForLatLngs() underzooms, so the map bounces back and forth as it iteratively settles on the correct bounds.
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
The way I approached this problem is by transformation matrices. Similar to what CATransform3D does with CALayers. So the way I formulated the problem is: assuming the rect (coord bounds) is centered on the screen at the greatest possible zoom with pitch=0 what transformation we need to apply so after applying the rotation, the rect ends up vertically centered and the resulting width equals the map width.
So the transformation needed would be: (Translation x Scale x Rotation x Perspective); where:
... t and s are the values we need to solve our system of equations for, p can be eye balled to 1 / -(view.height * 1.875) and a is the pitch angle in degrees. Note that the z dimension is zero on the scale matrix which will simplify the math a bit and we don't need the z dimension here.
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 h is the height of the bounds in pixels. After resolving this matrix we get:
Next in order to get the projection of a point from the non-tilted rect to the tilted rect we use a function P(x, y) which looks like:
Now the base equations for solving t and s are:
To fit to the width
The top-left point of the new transformed rect (in 2D) should match (height / 2 - newRectHeight / 2) to be in the center.
The width of the new transformed rect should match the container view.
.. where:
w is the width of the rect to be transformed
W is the width of the container view
h is the height of the rect to be transformed
Now if we solve the system of equations with all this information we obtain:
To fit to the height
The top-left point of new transformed rect should be (0, 0)
The height of the transformed rect should match the container view's height
... where h is the height of the rect.
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 t (see implementation)
Reacted by Linda Dong, Keith Smiley, Joe Engelman, Luke Tedman, Sam Soffes, Sander, Gabriel Lanata and Fidel Hernández Salazar
I don’t think #13007 addresses this issue at all. mbgl::Map::cameraForGeometry() is just a convenience wrapper for cameraForLatLngs(), so it suffers from the same inaccuracy when the map is tilted.
Activity
1ec5 commentedon Oct 16, 2015
To recap:
fitBoundsdoes not account for the trapezoidal shape of the bounds when the map is tilted. As a result, the map’s camera fails to round-trip correctly (tending to zoom out) when the map starts out tilted.[-]fix fitBounds in perspective view[/-][+]cameraForLatLngs fits to untilted bounds while in perspective view[/+]1ec5 commentedon Oct 20, 2015
This issue tracks the work to make
Map::cameraForLatLngs()visually fit to the passed-in bounds when the map is tilted. To illustrate the problem, I rigged iosapp to:visibleCoordinateBoundscamera, adjusting itspitch, and settingcamerato that modified camera)-[MGLMapView setVisibleCoordinateBounds:animated:]The result is that the map is zoomed much farther out than it should be:
ansis commentedon Oct 23, 2015
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.
1ec5 commentedon Oct 23, 2015
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.
[ios] Fix for #2259 - perspective tilt and cameraForLatLngs
1ec5 commentedon Jan 27, 2016
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),
cameraForLatLngs()underzooms, so the map bounces back and forth as it iteratively settles on the correct bounds.cammace commentedon Jun 29, 2016
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.
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:

cc: @zugaldia @ivovandongen
refs #5983: gate all viewport changes with equality check first
altafrm3d commentedon Dec 16, 2016
following this issue for: CameraUpdateFactory.newLatLngBounds() zooming to greatest possible zoom
Reflejo commentedon Jul 19, 2017
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
Preview
Implementation (example)
See this view controller
Explanation
The way I approached this problem is by transformation matrices. Similar to what
CATransform3Ddoes withCALayers. So the way I formulated the problem is: assuming the rect (coord bounds) is centered on the screen at the greatest possible zoom with pitch=0 what transformation we need to apply so after applying the rotation, the rect ends up vertically centered and the resulting width equals the map width.So the transformation needed would be: (Translation x Scale x Rotation x Perspective); where:
...
tandsare the values we need to solve our system of equations for,pcan be eye balled to1 / -(view.height * 1.875)andais the pitch angle in degrees. Note that thezdimension is zero on the scale matrix which will simplify the math a bit and we don't need thezdimension here.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
his the height of the bounds in pixels. After resolving this matrix we get:Next in order to get the projection of a point from the non-tilted rect to the tilted rect we use a function
P(x, y)which looks like:Now the base equations for solving
tandsare:To fit to the width
.. where:
wis the width of the rect to be transformedWis the width of the container viewhis the height of the rect to be transformedNow if we solve the system of equations with all this information we obtain:
To fit to the height
... where
his the height of the rect.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
t(see implementation)d-prukop commentedon Oct 2, 2018
see #13007. This may work for many of these cases.
1ec5 commentedon Oct 3, 2018
I don’t think #13007 addresses this issue at all.
mbgl::Map::cameraForGeometry()is just a convenience wrapper forcameraForLatLngs(), so it suffers from the same inaccuracy when the map is tilted.stale commentedon Apr 1, 2019
This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions.