编辑谷歌地图后获取坐标

时间:2012-07-03 06:10:27

标签: php javascript jquery google-maps

我将googlemap上标记的多边形的坐标存储到mysql数据库中。我可以在编辑页面中绘制该多边形。我的问题是,当我通过拖动点编辑地图时,我如何获得新的坐标。我阅读谷歌地图文档,用户可编辑的形状,但我无法得到它。请帮帮我。

提前致谢 iijb

2 个答案:

答案 0 :(得分:0)

请参阅Polygon.getPaths

的文档

Here is an example map获取路径(不是路径,因为折线没有该方法)并使用结果在文本框中创建xml。

答案 1 :(得分:0)

这个怎么样

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





    }

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