2016-03-31 196 views
-2

我看了這個帖子:How to get the center of a polygon in google maps v3?Matthew Scharley的答案目前標記爲正確的並不適合我。我從經緯度數組中動態地從服務器下載角度座標爲50-100個多邊形,並且我需要一個動態的非手動解決方案來確定多邊形中心。通過在同一個線程答案furiozo接近滿足我的需求,因爲它計算多邊形的平均中心,但是,我在我的console.log看到他的作用的結果:多邊形中心Google Maps JavaScript API v3

google.maps.Polygon.prototype.my_getBounds=function(){ 
    var bounds = new google.maps.LatLngBounds() 
    this.getPath().forEach(function(element,index){bounds.extend(element)}) 
    return bounds 
} 

console.log(myPolygon.my_getBounds().getCenter()); 

是不是我可以在我的程序中使用的東西。

我在控制檯中是這樣的:

_.M {} 
    lat: function() 
     arguments: (...) 
     caller: (...) 
     length: 0 
     name: "" 
     prototype: Object 
     __proto__: function() 
     <function scope> 
     Closure 
      a: 45.256705626 
      b: -75.91270512 
     Closure 
     Global: Window 
    lng: function() -- contains same sub-levels as lat: function() -- 
    __proto__: Object 

我真的需要現在的問題是如何獲得與第一閉合等級下坐在在LAT的一個& b緯度/經度值:函數()在我得到我的控制檯中的對象。 (我通過代碼手動檢查這些結果,它們代表了我的第一個多邊形的正確中心)。任何幫助如何去他們將不勝感激。

或者,如果有人知道如何動態獲取平均多邊形中心座標值的最新解決方案,以便最終結果將採用(lat,lng)格式,這可以節省我的一天。

非常感謝!

+1

你看到的是一個經緯度對象。爲什麼不使用'myPolygon.my_getBounds()。getCenter()。lat()'和'myPolygon.my_getBounds()。getCenter()。lng()',還是我誤解了請求? – duncan

+0

@鄧肯哇,謝謝!這很簡單!我花了4個小時研究這個。是的,這完全解決了我的問題。我是新來的,所以我不知道如何處理這個LatLng對象。再一次非常感謝你! – PunainenAurinko

回答

0

你看到的是一個LatLng對象。

你可以做的是簡單地調用就可以了lat()lng()功能:

var lat = myPolygon.my_getBounds().getCenter().lat(); 
var lng = myPolygon.my_getBounds().getCenter().lng(); 
相關問題