我需要知道如何檢索谷歌地圖API V3可視縮放級別的半徑。半徑
例如,如果我在縮放級別3,並根據用戶的屏幕尺寸(讓剛剛說400x400的可視區域)?我如何獲得可視區域的「半徑」圈子。
另外,我目前正在使用map.fitBounds()爲所有點添加到地圖,所以我真的所有我需要是所有邊界的半徑。我想要的是類似於「20英里」的東西,我可以將其輸入到我的數據庫應用程序中。
我需要知道如何檢索谷歌地圖API V3可視縮放級別的半徑。半徑
例如,如果我在縮放級別3,並根據用戶的屏幕尺寸(讓剛剛說400x400的可視區域)?我如何獲得可視區域的「半徑」圈子。
另外,我目前正在使用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()
將包含完整的地圖視後。但是,如果你LatLngBounds
被延伸到包括LatLng
點,然後你發出一個map.fitBounds(bounds)
建成,API增加了地圖的視口的一個位,所以bounds
「盒子」具有一定的填充。
如果使用地圖目前的檢視,從中心到視口的拐角半徑可能比你想有一個較大的半徑。也許從視口中心到最遠視口邊緣中間的距離。 (如果地圖不是完美的正方形)
我相信有getNorthEast()和getSouthWest()方法,但是這會給你一個矩形(其界定確實是),你可以比計算距離這兩個之間等 要計算出圓那rectandle可能是一些工作......
也許這將幫助:http://code.google.com/apis/maps/articles/mvcfun.html
和示例:http://code.google.com/apis/maps/articles/mvcfun/step6.html
重構版本Eric's answer以上使用google.maps.geometry.spherical命名空間(請確保您已加載Geometry library以使其工作)。
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);
}
完美!幫助我很多地方要求低到不要達到配額太快。 – kosemuckel 2016-11-22 18:03:38
我也重構了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
}
Awesomesauce,正是我需要的。謝謝! – 2010-08-20 15:54:23
爲了得到這個例子的工作,前3行應該是 'var bounds = map.getBounds(); var center = bounds.getCenter(); var ne = bounds.getNorthEast();' – 2015-04-09 14:04:32