Google Maps V3 - 如何计算给定边界的缩放级别 [英] Google Maps V3 - How to calculate the zoom level for a given bounds

查看:25
本文介绍了Google Maps V3 - 如何计算给定边界的缩放级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用 Google Maps V3 API 计算给定边界缩放级别的方法,类似于 V2 API 中的 getBoundsZoomLevel().

I'm looking for a way to calculate the zoom level for a given bounds using the Google Maps V3 API, similar to getBoundsZoomLevel() in the V2 API.

这是我想要做的:

// These are exact bounds previously captured from the map object
var sw = new google.maps.LatLng(42.763479, -84.338918);
var ne = new google.maps.LatLng(42.679488, -84.524313);
var bounds = new google.maps.LatLngBounds(sw, ne);
var zoom = // do some magic to calculate the zoom level

// Set the map to these exact bounds
map.setCenter(bounds.getCenter());
map.setZoom(zoom);

// NOTE: fitBounds() will not work

不幸的是,我无法将 fitBounds() 方法用于我的特定用例.它适用于在地图上拟合标记,但不适用于设置精确边界.下面是我为什么不能使用 fitBounds() 方法的示例.

Unfortunately, I can't use the fitBounds() method for my particular use case. It works well for fitting markers on the map, but it does not work well for setting exact bounds. Here is an example of why I can't use the fitBounds() method.

map.fitBounds(map.getBounds()); // not what you expect

推荐答案

一个类似的问题已在 Google 群组中提出:http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/e6448fc197c3c892

A similar question has been asked on the Google group: http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/e6448fc197c3c892

缩放级别是离散的,每一步的比例都加倍.因此,一般而言,您无法完全满足您想要的边界(除非您对特定的地图尺寸非常幸运).

The zoom levels are discrete, with the scale doubling in each step. So in general you cannot fit the bounds you want exactly (unless you are very lucky with the particular map size).

另一个问题是边长之间的比率,例如您无法将边界精确地拟合到方形地图内的细矩形中.

Another issue is the ratio between side lengths e.g. you cannot fit the bounds exactly to a thin rectangle inside a square map.

如何拟合精确的边界没有简单的答案,因为即使你愿意改变地图 div 的大小,你也必须选择你改变到的大小和相应的缩放级别(粗略地说,你做到了吗?比现在大还是小?).

There's no easy answer for how to fit exact bounds, because even if you are willing to change the size of the map div, you have to choose which size and corresponding zoom level you change to (roughly speaking, do you make it larger or smaller than it currently is?).

如果你真的需要计算缩放而不是存储它,这应该可以解决问题:

If you really need to calculate the zoom, rather than store it, this should do the trick:

墨卡托投影会扭曲纬度,但经度的任何差异始终代表地图宽度的相同部分(角度差异,以度数/360 为单位).缩放为零时,整个世界地图为 256x256 像素,缩放每个级别会使宽度和高度加倍.因此,经过一些代数运算后,我们可以按如下方式计算缩放比例,前提是我们知道地图的宽度(以像素为单位).请注意,因为经度是环绕的,所以我们必须确保角度是正的.

The Mercator projection warps latitude, but any difference in longitude always represents the same fraction of the width of the map (the angle difference in degrees / 360). At zoom zero, the whole world map is 256x256 pixels, and zooming each level doubles both width and height. So after a little algebra we can calculate the zoom as follows, provided we know the map's width in pixels. Note that because longitude wraps around, we have to make sure the angle is positive.

var GLOBE_WIDTH = 256; // a constant in Google's map projection
var west = sw.lng();
var east = ne.lng();
var angle = east - west;
if (angle < 0) {
  angle += 360;
}
var zoom = Math.round(Math.log(pixelWidth * 360 / angle / GLOBE_WIDTH) / Math.LN2);

这篇关于Google Maps V3 - 如何计算给定边界的缩放级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆