Mec*_*are 38 google-maps google-maps-api-3
我需要知道如何在google maps API v3中检索可视缩放级别的半径.
例如,如果我处于缩放级别3,并且取决于用户屏幕大小(仅举例说400x400可视区域),我如何获得可视区域的"半径"圆.
或者我正在使用map.fitBounds()来为我添加到地图中的所有点,所以我真正需要的是所有边界的半径.我想要的是"20英里",我可以将其输入我的数据库应用程序.
Eri*_*c C 81
半径等于从边界中心到边界角之一的距离.使用本页计算中的Great Circle Distance公式,我想出了以下内容:
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));
Run Code Online (Sandbox Code Playgroud)
用这个玩了一些之后,我注意到map.getBounds()它将包含完整的地图视口.但是如果你LatLngBounds是通过扩展包含LatLng点来构建的,然后你发出一个map.fitBounds(bounds),那么api会增加地图的视口,以便bounds"box"有一些填充.
如果使用地图的当前视口,则从中心到视口角的半径可能比您想要的半径更长.也许是从视口中心到最远视口边缘中间的距离.(如果地图不是完美的正方形)
Rus*_*lin 26
使用google.maps.geometry.spherical命名空间重构Eric的上述答案版本(确保加载Geometry库以使其工作).
var bounds = map.getBounds();
var center = map.getCenter();
if (bounds && center) {
var ne = bounds.getNorthEast();
// Calculate radius (in meters).
var radius = google.maps.geometry.spherical.computeDistanceBetween(center, ne);
}
Run Code Online (Sandbox Code Playgroud)
我还重构了 Eric 的答案,我的是作为一个独立的函数,并以米为单位返回结果(根据Places API search 的需要。
function getBoundsRadius(bounds){
// r = radius of the earth in km
var r = 6378.8
// degrees to radians (divide by 57.2958)
var ne_lat = bounds.getNorthEast().lat() / 57.2958
var ne_lng = bounds.getNorthEast().lng() / 57.2958
var c_lat = bounds.getCenter().lat() / 57.2958
var c_lng = bounds.getCenter().lng() / 57.2958
// distance = circle radius from center to Northeast corner of bounds
var r_km = r * Math.acos(
Math.sin(c_lat) * Math.sin(ne_lat) +
Math.cos(c_lat) * Math.cos(ne_lat) * Math.cos(ne_lng - c_lng)
)
return r_km *1000 // radius in meters
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24441 次 |
| 最近记录: |