How to Calculate Scale to Zoom ratio for Google Static map Images
Asked Answered
L

2

6

I am currently working with Google Static maps images and i wish to find out how to calculate the scale for a given zoom level. I know Google maps use Mercator Projections. The application I am making gets a satellite map image from the Google and a user draws a polygon on top of it. I need to calculate the area of this polygon in real world units. Any suggestions???

Lacerate answered 13/9, 2011 at 18:28 Comment(0)
R
3

As stated at Google Maps Documentation:

Because the basic Mercator Google Maps tile is 256 x 256 pixels.
Also note that every zoom level, the map has 2 n tiles.

Meaning that at zoomLevel 2,the pixels in any direction of a map are = 256 * 2² = 1024px.

Taking into account that the earth has a perimeter of ~40,000 kilometers, in zoom 0, every pixel ~= 40,000 km/256 = 156.25 km
At zoom 9, pixels are 131072: 1px = 40,000 km / 131072 = 0.305 km ... and so on.

If we want 400px = 1km, we have to choose the closest approximation possible, so: 1px = 1km/400 = 0.0025km

I tried zoom = 15 and obtained 1px = 0.00478 and zoom = 16 that gave me 1px = 0.00238km

Meaning that you should use zoom = 16, and you will have 0.955km every 400px in the Equator line and only for x coordinates.

As you go north or south in latitude, perimeter is everytime smaller, thus changing the distance. And of course it also changes the correlation in the y axis as the projection of a sphere is tricky.
If you want to calculate with a function the exact distance, you should use the one provided by Google at their documentation:

// Describe the Gall-Peters projection used by these tiles.
  gallPetersMapType.projection = {
    fromLatLngToPoint: function(latLng) {
      var latRadians = latLng.lat() * Math.PI / 180;
      return new google.maps.Point(
          GALL_PETERS_RANGE_X * (0.5 + latLng.lng() / 360),
          GALL_PETERS_RANGE_Y * (0.5 - 0.5 * Math.sin(latRadians)));
    },
    fromPointToLatLng: function(point, noWrap) {
      var x = point.x / GALL_PETERS_RANGE_X;
      var y = Math.max(0, Math.min(1, point.y / GALL_PETERS_RANGE_Y));

      return new google.maps.LatLng(
          Math.asin(1 - 2 * y) * 180 / Math.PI,
          -180 + 360 * x,
          noWrap);
    }
  };
Retrospect answered 22/6, 2016 at 14:52 Comment(0)
M
0

You can try this:

public static int GetWebMercatorZoomLevelFromMapScale(double currentMapScale)
{
    var scales = Enumerable.Range(1, 25)
        .Select(x => 591657550.500000 / Math.Pow(2, x - 1))
        .ToArray();

    int zoomLevel = Array.IndexOf(scales, scales.OrderBy(number => Math.Abs(number - currentMapScale)).First());

    return zoomLevel;
}
Metabolize answered 11/10, 2018 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.