如何在谷歌地图apiv3中获取新编辑的多边形坐标 [英] how to get the newly edited polygon coordinates in google map apiv3

查看:119
本文介绍了如何在谷歌地图apiv3中获取新编辑的多边形坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功创建和编辑了多边形,在绘制了多边形之后,我得到了多边形的坐标,我要寻找的是在编辑了多边形之后,我没有得到多边形的新坐标(在拖动结束后,我是获取坐标后,新坐标应立即出现,就像我绘制多边形时一样.)

i have created and edited polygon successfully and after drawing the polygon i am getting the coordinates of the polygon, what i am looking for is after editing the polygon i am not getting the new coordinates of the polygon(after drag end i am getting the coordinates, immediately the new coordinates should appear as when i draw the polygon happen).

我的Java脚本:

var map;
    var coords = [];
    var lats = [];
    var lngs = [];
    var myMarkers = [];
    var myPoly;
    function initialize() {
        var latlng = new google.maps.LatLng(17.397821, 78.479354);
        var mapOptions = {
            zoom: 15,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.TERRAIN
        }
        map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
        addEventListener_marker();

    }
    function addEventListener_marker() {
        google.maps.event.addListener(map, 'click', function (e) {
            coords.push(e.latLng);
            lats.push(e.latLng.lb);
            lngs.push(e.latLng.mb);
            placeMarker(e.latLng, map);
        });
    }

    function placeMarker(position, map) {
        var mark = new google.maps.Marker({
            position: position,
            map: map,
            draggable: false
        });
        myMarkers.push(mark);
    }

    function createPoly() {
        polyOptions = {
            path: coords,
            strokeColor: "#FF0000",
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: "#FF6803",
            fillOpacity: '0.25',
            draggable: true,
            editable: true
        }
        myPoly = new google.maps.Polygon(polyOptions);
        myPoly.setMap(map);
        google.maps.event.addListener(myPoly, 'click', isWithinPoly);
        markerCoords();
        //removeEventListener();
        clearOverlays();

    }

    function markerCoords() {
        var curLatLng = [];
        curLatLng = myPoly.getPath().getArray();
        $("#info").append("coordinates are: Latitude: " + curLatLng + "<br/>");
        //google.maps.event.addListener(myPoly, 'shape_changed', function () {
        google.maps.event.addListener(myPoly, 'dragend', function () {
            //alert("drag end");
            var curLatLng;
            curLatLng = myPoly.getPath().getArray();
            $("#info").append("coordinates are: Latitude: " + curLatLng + "<br/>");
        });

        google.maps.event.addListener(myPoly, 'set_at', function () {
            //alert("drag end");
            var curLatLng1;
            curLatLng1 = myPoly.getPath().getArray();
            $("#info").append("coordinates are: Latitude: " + curLatLng1 + "<br/>");
        });

        google.maps.event.addListener(myPoly, 'insert_at', function () {
            //alert("drag end");
            var curLatLng2;
            curLatLng2 = myPoly.getPath().getLength();
            $("#info").append("coordinates are: Latitude: " + curLatLng2 + "<br/>");
        });

    }

    function removeEventListener() {
        //google.maps.event.clearListeners(map, 'bounds_changed');
    }
    function clearOverlays() {
        setAllMap(null);
    }
    function setAllMap(map) {
        for (var i = 0; i < myMarkers.length; i++) {
            myMarkers[i].setMap(map);
        }
    }
    function isWithinPoly(event) {
        var isWithinPolygon = google.maps.geometry.poly.containsLocation(event.latLng, this);
    }

    google.maps.event.addDomListener(window, 'load', initialize); 

和html是:

<div id="map-canvas"></div>
<p id="info"></p>

<input type="button" onclick="createPoly();" value="draw polygon" />



我正在使用set_at和其他两个,但是三个事件均未触发.

i am using set_at and other two like this but none of the three events are fired.

function markerCoords() {
        var curLatLng = [];
        curLatLng = myPoly.getPath().getArray();
        $("#info").append("coordinates are: Latitude: " + curLatLng + "<br/>");
        //google.maps.event.addListener(myPoly, 'shape_changed', function () {
        google.maps.event.addListener(myPoly, 'dragend', function () {
            alert("drag end");
            var curLatLng;
            curLatLng = myPoly.getPath().getArray();
            $("#info").append("coordinates are: Latitude: " + curLatLng + "<br/>");
        });

        google.maps.event.addListener(myPoly, 'set_at', function () {
            alert("set_at");
            var curLatLng;
            curLatLng = myPoly.getPath().getArray();
            $("#info").append("coordinates are: Latitude: " + curLatLng + "<br/>");
        });

        google.maps.event.addListener(myPoly, 'insert_at', function () {
            alert("insert_at");
            var curLatLng;
            curLatLng = myPoly.getPath().getArray();
            $("#info").append("coordinates are: Latitude: " + curLatLng + "<br/>");
        });

        google.maps.event.addListener(myPoly, 'remove_at', function () {
            alert("remove_at");
            var curLatLng;
            curLatLng = myPoly.getPath().getArray();
            $("#info").append("coordinates are: Latitude: " + curLatLng + "<br/>");
        });
    }

推荐答案

观察多边形路径的事件set_atinsert_atremove_at:

Observe the events set_at, insert_at and remove_at of the polygon-path:

    google.maps.event.addListener(myPoly.getPath(), 'set_at',   markerCoords);
    google.maps.event.addListener(myPoly.getPath(), 'insert_at',markerCoords);
    google.maps.event.addListener(myPoly.getPath(), 'remove_at',markerCoords);

这篇关于如何在谷歌地图apiv3中获取新编辑的多边形坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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