我需要知道如何在 google map API v3 中检索可见缩放级别的半径。
例如,如果我处于缩放级别 3,并且根据用户屏幕尺寸(假设为 400x400 可视区域),如何获取可视区域的“半径”圆。
或者,我目前正在对添加到 map 中的所有点使用map.fitBounds(),所以我实际上需要的是所有边界的半径。我想要的是像“20 英里”这样的东西,我可以将其输入到我的数据库应用程序中。
最佳答案
半径等于从边界中心到边界角之一的距离。使用 the calculations from this page 中的大圆距离公式,我想出了以下内容:
var bounds = map.getBounds();
var center = bounds.getCenter();
var ne = bounds.getNorthEast();
// r = radius of the earth in statute miles
var r = 3963.0;
// Convert lat or lng from decimal degrees into radians (divide by 57.2958)
var lat1 = center.lat() / 57.2958;
var lon1 = center.lng() / 57.2958;
var lat2 = ne.lat() / 57.2958;
var lon2 = ne.lng() / 57.2958;
// distance = circle radius from center to Northeast corner of bounds
var dis = r * Math.acos(Math.sin(lat1) * Math.sin(lat2) +
Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1));
在玩了一些之后,我注意到 map.getBounds()
将包含完整的 map 视口(viewport)。但如果你的LatLngBounds
通过扩展来构建 LatLng
点,然后发出 map.fitBounds(bounds)
,API 会稍微增加 map 的视口(viewport),以便 bounds
“框”有一些填充。
如果您使用 map 的当前视口(viewport),从视口(viewport)中心到角点的半径可能会比您想要的更长。也许是从视口(viewport)中心到最远视口(viewport)边缘中间的距离。 (如果 map 不是完美的正方形)
关于google-maps - 谷歌地图 v3 中 "viewable"区域的半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3525670/