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

发布于 2024-12-04 15:55:13 字数 134 浏览 0 评论 0原文

我目前正在使用 Google 静态地图图像,我希望了解如何计算给定缩放级别的比例。我知道谷歌地图使用墨卡托投影。我正在制作的应用程序从 Google 获取卫星地图图像,用户在其顶部绘制一个多边形。我需要以现实世界单位计算该多边形的面积。有什么建议吗???

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???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛时过境迁 2024-12-11 15:55:13

Google 地图文档所述:

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

这意味着在缩放级别 2 时,地图任意方向的像素 = 256 * 2² = 1024px。

考虑到地球周长约为 40,000 公里,在变焦 0 时,每个像素 ~= 40,000 公里/256 = 156.25 公里
在变焦 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,那么你每次将有0.955公里400 像素在赤道线上并且仅适用于x 坐标

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

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);
    }
  };
蝶舞 2024-12-11 15:55:13

你可以试试这个:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文