We want to display several markers on a static map and want to calculate the optimal zoom-level like Google Maps does. We already calculated the bounding rectangle and the centerpoint of the map but now we have a hard time to calculate the correct zoomlevel to display the whole bounding rectangle. Can someone please point us into the right direction?
Asked
Active
Viewed 3.1k times
16
-
It's not very sophisticated, but why not just multiply the MBR by 120% and zoom to that? Anything else depends on your definition of "optimal", doesn't it? – ThomM Feb 02 '12 at 00:06
-
+1 to ThomM's idea. This is exactly what ArcGIS does. – Ragi Yaser Burhum Feb 02 '12 at 00:17
-
2Sorry, but what is this MBR? – Rodrigo Feb 11 '15 at 23:52
-
MBR = minimum bounding rectangle, better known as the bounding box, i.e. the smallest rectangular area that spatially contains the collection of some thing. – alphabetasoup Aug 15 '21 at 22:17
3 Answers
8
To get the zoom level, you'll need to know the pixel dimensions of your map. You'll also need to do your math in spherical mercator coordinates.
- Convert latitude, longitude to spherical mercator x, y.
- Get distance between your two points in spherical mercator.
- The equator is about 40m meters long projected and tiles are 256 pixels wide, so the pixel length of that map at a given zoom level is about 256 * distance/40000000 * 2^zoom. Try zoom=0, zoom=1, zoom=2 until the distance is too long for the pixel dimensions of your map.
Michal Migurski
- 1,295
- 7
- 12
7
This is the C# code I use in Maperitive:
public void ZoomToArea (Bounds2 mapArea, float paddingFactor)
{
double ry1 = Math.Log((Math.Sin(GeometryUtils.Deg2Rad(mapArea.MinY)) + 1)
/ Math.Cos(GeometryUtils.Deg2Rad(mapArea.MinY)));
double ry2 = Math.Log((Math.Sin(GeometryUtils.Deg2Rad(mapArea.MaxY)) + 1)
/ Math.Cos(GeometryUtils.Deg2Rad(mapArea.MaxY)));
double ryc = (ry1 + ry2) / 2;
double centerY = GeometryUtils.Rad2Deg(Math.Atan(Math.Sinh(ryc)));
double resolutionHorizontal = mapArea.DeltaX / Viewport.Width;
double vy0 = Math.Log(Math.Tan(Math.PI*(0.25 + centerY/360)));
double vy1 = Math.Log(Math.Tan(Math.PI*(0.25 + mapArea.MaxY/360)));
double viewHeightHalf = Viewport.Height/2.0f;
double zoomFactorPowered = viewHeightHalf
/ (40.7436654315252*(vy1 - vy0));
double resolutionVertical = 360.0 / (zoomFactorPowered * 256);
double resolution = Math.Max(resolutionHorizontal, resolutionVertical)
* paddingFactor;
double zoom = Math.Log(360 / (resolution * 256), 2);
double lon = mapArea.Center.X;
double lat = centerY;
CenterMapOnPoint(new PointD2(lon, lat), zoom);
}
mapArea: bounding box in long/lat coords (x=long, y=lat)paddingFactor: this can be used to get the "120%" effect ThomM refers to. Value of 1.2 would get you the 120%.
Note that in my case zoom can be a real number. In the case of Web maps, you need a integer zoom value, so you should use something like (int)Math.Floor(zoom) to get it.
Of course, this code only applies to Web Mercator projection.
Igor Brejc
- 3,677
- 29
- 28
-
1
-
Perfect! I've used this to calculate zoom of BoundingBox in OsmDroid SDK and it works :) – Billda Feb 09 '18 at 15:35
-
Where do I find these libraries? I can't find a GeometryUtils or Bounds2 assembies. Do I need to download something from maperitive? I only see an app there. – Dowlers Jul 03 '19 at 17:55
-
@Dowlers
GeometryUtilsis just used here to convert degrees to radians and back, it's a simple math formula.Bounds2is basically just a rectangle structure. This code was provided more as a pseudocode than something directly copy-pastable. – Igor Brejc Jul 12 '19 at 07:37 -
Ahh got you. I have to admit that after I asked the question I did some poking around the maperitive app and found the .dlls I needed. But I noticed the licensing restrictions so I went another route. I used the code from this article instead: https://rbrundritt.wordpress.com/2009/07/21/determining-best-map-view-for-an-array-of-locations/ – Dowlers Jul 12 '19 at 20:34
-
1This is awesome @IgorBrejc- thank you so much! I converted it to python here in case others are writing python code: https://gist.github.com/mappingvermont/d534539fa3ebe4a1e242644e528bf7b9 – Charlie Lefrak Apr 21 '20 at 14:50
1
If you are using OpenLayers then Map.getZoomForExtent will calculate the highest zoom level that can fit the entire extent on the map. The extent needs to be in the map's projection. You can also use a fill_factor to avoid displaying points on the edge of the map, and max_zoom to limit the possible zoom:
extent = extent.scale(1/fill_ratio);
var zoom = Math.min(map.getZoomForExtent(map_extent), max_zoom);
map.setCenter(extent.getCenterLonLat(), zoom);
Alex Morega
- 291
- 2
- 5
-
this didn't fix my issue, but it lead me to find Leaflet's fitBounds method, which saved my day. – SamuelDev Feb 05 '20 at 11:19