2013-07-26 58 views
0

我正在使用Google Maps API v3,並且想要獲取地圖中心和邊界框右邊界之間的距離。從Lat和Lng獲取單個GMaps座標

我當前的代碼如下所示:

var EX1 = map.getCenter(); 
var EX2 = "(" + map.getCenter().lat() + ", " + map.getBounds().getNorthEast().lng() + ")"; 
var Radius = google.maps.geometry.spherical.computeDistanceBetween(EX1, EX2)/1000; 
alert(EX1); 
alert(EX2); 
alert(Radius); 

EX1和EX2似乎有相同的格式,EX2的座標看起來是正確的,但半徑警報總是顯示爲NaN(它正常工作與var EX2 = map.getBounds().getNorthEast();)。

有沒有辦法獲得中心和右邊界之間的距離?

感謝,

蒂莫

回答

1

EX2是一個字符串。 EX1是一個google.maps.LatLng對象,它具有將其轉換爲字符串的方法。它們不是「相同的格式」。這應該工作(未測試):

var EX1 = map.getCenter(); 
var EX2 = new google.maps.LatLng(map.getCenter().lat(),map.getBounds().getNorthEast().lng()); 
var Radius = google.maps.geometry.spherical.computeDistanceBetween(EX1, EX2)/1000; 
alert(EX1); 
alert(EX2); 
alert(Radius); 
相關問題