如何在谷歌地图apiv3中获取新编辑的多边形坐标

时间:2013-11-27 12:41:16

标签: javascript jquery html google-maps-api-3

我已经成功创建和编辑了多边形,并且在绘制多边形后我得到了多边形的坐标,我正在寻找的是在编辑多边形之后我没有得到多边形的新坐标(在拖动结束后我是获取坐标时,新坐标应立即显示为绘制多边形时的情况。)

我的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和其他两个这样的但是这三个事件都没有被触发。

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

1 个答案:

答案 0 :(得分:2)

观察多边形路径的事件set_atinsert_atremove_at

    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);