2013-02-19 159 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 += ")";