2013-02-19 157 views
5

我試圖檢索折線和多邊形的latlng座標。完成任一對象的繪製後,我想將latlng存儲在數據庫中,但現在我只是試圖在textarea中顯示latlng。對於標記,矩形和圓形,我已經很容易做到了這一點,但折線和多邊形的術語讓我感到困惑。當我完成繪圖工作時,我使用了一個addDomListener(drawingManager,'polygoncomplete',...對於多邊形,然後遍歷我繪製的所有多邊形,然後對每個多邊形進行迭代,然後遍歷它的座標數組。並試圖將谷歌文檔頁面上的百慕大三角例子。 Bermuda Example 我已經閱讀過的文檔很多次,只是不能看,我失去了什麼。任何幫助表示讚賞。getpaths()多邊形谷歌地圖API

//Save polygons data to text area 
       var polygons = []; 

       google.maps.event.addDomListener(drawingManager, 'polygoncomplete', function(polygon) { 
        polygons.push(polygon); 
       }); 

       google.maps.event.addDomListener(savebutton, 'click', function() { 
        document.getElementById("savedatapolygon").value = ""; 
        for (var i = 0; i < polygons.length; i++) { 
         var polygonBounds = polygons[i].getPath(); 
         var xy; 
         // Iterate over the polygonBounds vertices. 
         for (var i = 0; i < polygonBounds.length; i++) { 
          xy = polygonBounds.getAt(i); 
          contentString += '<br>' + 'Coordinate: ' + i + '<br>' + xy.lat() +',' + xy.lng(); 
         } 
         document.getElementById("savedatapolygon").value += "polygon("; 
         document.getElementById("savedatapolygon").value += contentString; 
         document.getElementById("savedatapolygon").value += ")"; 

        } 
     }); 
+0

你得到了哪些javascript錯誤? [This example](http://www.geocodezip.com/blitz-gmap-editor/test5.html)可能對你有所幫助,它將所有來自DrawingManager的對象從JSON或KML中導出。 – geocodezip 2013-02-19 01:40:28

+0

完美!這將節省我很多時間。我在工作中使用textpad,因爲這是我所有可用的,我不能下載任何其他東西,所以我不能跟蹤任何錯誤:/ – ddalbus 2013-02-19 16:55:15

+0

大多數瀏覽器都有一個javascript控制檯報告錯誤,調試器更簡單,但看到報告的錯誤有時候是你所需要的。 – geocodezip 2013-02-19 16:57:47

回答

16

Google Maps Polygon類返回MVCArray 。您需要使用MVCArrayforEach方法循環訪問。

var polygonBounds = polygons[i].getPath(); 
// Iterate over the polygonBounds vertices. 
polygonBounds.forEach(function(xy, i) { 
    contentString += '<br>' + 'Coordinate: ' + i + '<br>' + xy.lat() +',' + xy.lng(); 
}); 
document.getElementById("savedatapolygon").value += "polygon("; 
document.getElementById("savedatapolygon").value += contentString; 
document.getElementById("savedatapolygon").value += ")";