getpaths()多边形谷歌地图API [英] getpaths() polygons google maps api

查看:430
本文介绍了getpaths()多边形谷歌地图API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检索折线和多边形的latlng坐标。完成任一对象的绘制后,我想将latlng存储在数据库中,但现在我只是试图在textarea中显示latlng。对于标记,矩形和圆形,我已经很容易做到了这一点,但多义线和多边形的术语让我感到困惑。当我完成绘图工作时,我使用了一个addDomListener(drawingManager,'polygoncomplete',...对于多边形,然后遍历我绘制的所有多边形,然后对每个多边形进行迭代,然后遍历它的坐标数组。并在Google文档页面上尝试了百慕大三角形示例。
百慕大示例
我已经阅读了很多次文档,只是看不到我缺少的东西,任何帮助表示感谢。

I am attempting to retrieve the latlng coordinates of both a polyline and polygon. After I complete the drawing of either object I would like to store the latlng in a database, but for now I am merely trying to show the latlng in a textarea. I have done this easily enough for markers, rectangles, and circles, but the terminology for polylines and polygons is baffling me. When I complete the drawing i use an addDomListener(drawingManager, 'polygoncomplete', ... for polyons and then iterate through all the polygons i have drawn. For each polygon I then iterate through its array of coords. I have searched this forums database and tried the Bermuda triangle example on the Google documentation page. Bermuda Example I have read over the documentation many times and just cant see what i am missing. Any help is appreciated.

//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 += ")";

                    }
        });


推荐答案

Google Maps Polygon 类会返回一个均为MVCArray 。您需要使用 MVCArray < forEach / $>来遍历它。

The Google Maps Polygon class returns a MVCArray. You need to use the forEach method of the MVCArray to loop through it.

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 += ")";

这篇关于getpaths()多边形谷歌地图API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆