简体   繁体   English

如何计算Google静态地图图像的缩放比例

[英]How to Calculate Scale to Zoom ratio for Google Static map Images

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. 我正在制作的应用程序从Google获取卫星地图图像,用户在其上绘制多边形。 I need to calculate the area of this polygon in real world units. 我需要以实际单位计算这个多边形的面积。 Any suggestions??? 有什么建议么???

As stated at Google Maps Documentation : Google地图文档中所述

Because the basic Mercator Google Maps tile is 256 x 256 pixels. 因为基本的墨卡托谷歌地图图块是256 x 256像素。
Also note that every zoom level, the map has 2 n tiles. 另请注意,每个缩放级别,地图都有2 n个图块。

Meaning that at zoomLevel 2,the pixels in any direction of a map are = 256 * 2² = 1024px. 这意味着在zoomLevel 2中,地图任何方向上的像素都是= 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 考虑到地球周长约为40,000公里,变焦0,每个像素〜= 40,000 km / 256 = 156.25 km
At zoom 9, pixels are 131072: 1px = 40,000 km / 131072 = 0.305 km ... and so on. 在缩放9处,像素为131072:1px = 40,000 km / 131072 = 0.305 km ......依此类推。

If we want 400px = 1km, we have to choose the closest approximation possible, so: 1px = 1km/400 = 0.0025km 如果我们想要400px = 1km,我们必须选择最接近的近似值,因此:1px = 1km / 400 = 0.0025km

I tried zoom = 15 and obtained 1px = 0.00478 and zoom = 16 that gave me 1px = 0.00238km 我尝试zoom = 15并获得1px = 0.00478和zoom = 16,这给了我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 . 这意味着您应该使用zoom = 16,并且在Equator线中每400px将有0.955km并且仅用于x坐标

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. 当然,它也会改变y轴的相关性,因为球体的投影很棘手。
If you want to calculate with a function the exact distance, you should use the one provided by Google at their documentation : 如果您想使用函数计算确切距离,则应使用Google提供的文档

// 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);
    }
  };

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM