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

时间:2011-09-13 18:28:33

标签: java zoom google-static-maps

我目前正在使用Google静态地图图片,我希望了解如何计算给定缩放级别的比例。我知道谷歌地图使用墨卡托预测。我正在制作的应用程序从Google获取卫星地图图像,用户在其上绘制多边形。我需要以实际单位计算这个多边形的面积。任何建议???

2 个答案:

答案 0 :(得分:0)

Google Maps Documentation所述:

  

因为基本墨卡托谷歌地图图块是256 x 256像素   另请注意,每个缩放级别,地图都有2个 n 图块。

这意味着在zoomLevel 2中,地图任意方向的像素都是= 256 *2²= 1024px。

考虑到地球的周长约为40,000公里,变焦0,每个像素〜= 40,000 km / 256 = 156.25 km
在缩放9处,像素为131072:1px = 40,000 km / 131072 = 0.305 km ......依此类推。

如果我们想要400px = 1km,我们必须选择最接近的近似值,因此:1px = 1km / 400 = 0.0025km

我尝试了zoom = 15并获得1px = 0.00478和zoom = 16,这给了我1px = 0.00238km

意味着您应该使用zoom = 16,并且在赤道线中每400px 将有0.955km,仅用于 x坐标

当你在纬度上向北或向南走时,周长每次都会变小,从而改变距离。当然,它也会改变 y轴的相关性,因为球体的投影很棘手。
如果您想使用函数计算确切距离,则应使用Google在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);
    }
  };

答案 1 :(得分:0)

您可以尝试以下方法:

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