最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I capture point locations of Polygon with Google Maps API? - Stack Overflow

programmeradmin3浏览0评论

Using the Google Maps API Drawing Manager, I want to collect the location of each point in the polygon that is drawn by a user.

I know there is a getPath() function, but I'm not sure where I use it.

This is all the code I have so far:

var map; var drawingManager;

function initialize() {
    var mapOptions = {
        center: new google.maps.LatLng(45.13536, -100.762952),
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    drawingManager = new google.maps.drawing.DrawingManager({
        drawingMode: google.maps.drawing.OverlayType.POLYGON,
        drawingControl: false,
        polygonOptions: {
            fillColor: "#000000",
            fillOpacity: .6,
            strokeWeight: 1,
            strokeColor: "#666666",
            clickable: false,
            editable: true,
            zIndex: 1
        }
    });

    drawingManager.setMap(map);

    google.maps.event.addListener(drawingManager, 'polygonplete', function(polygon) {
        drawingManager.setDrawingMode(null);
    });
}

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

So from this code, how can I use the getPath() function to show the coordinates that make up the Polygon that is drawn?

Using the Google Maps API Drawing Manager, I want to collect the location of each point in the polygon that is drawn by a user.

I know there is a getPath() function, but I'm not sure where I use it.

This is all the code I have so far:

var map; var drawingManager;

function initialize() {
    var mapOptions = {
        center: new google.maps.LatLng(45.13536, -100.762952),
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    drawingManager = new google.maps.drawing.DrawingManager({
        drawingMode: google.maps.drawing.OverlayType.POLYGON,
        drawingControl: false,
        polygonOptions: {
            fillColor: "#000000",
            fillOpacity: .6,
            strokeWeight: 1,
            strokeColor: "#666666",
            clickable: false,
            editable: true,
            zIndex: 1
        }
    });

    drawingManager.setMap(map);

    google.maps.event.addListener(drawingManager, 'polygonplete', function(polygon) {
        drawingManager.setDrawingMode(null);
    });
}

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

So from this code, how can I use the getPath() function to show the coordinates that make up the Polygon that is drawn?

Share Improve this question edited May 8, 2018 at 15:03 ianbroad asked Sep 24, 2013 at 1:33 ianbroadianbroad 1112 gold badges7 silver badges22 bronze badges 1
  • possible duplicate of How to get point coordinates of a modified drawingManager shape? GoogleMaps API v3 – geocodezip Commented Sep 24, 2013 at 1:35
Add a ment  | 

1 Answer 1

Reset to default 17

use it inside the polygonplete-callback.

example:

 google.maps.event.addListener(drawingManager, 'polygonplete', function(polygon) {
    drawingManager.setDrawingMode(null);
    var arr=[];
    polygon.getPath().forEach(function(latLng){arr.push(latLng.toString());})
    alert(arr.join(',\n'));
});

it iterates over the path(using the forEach-method of a google.maps.MVCArray) and populates another array with the string-representations of the LatLng's

发布评论

评论列表(0)

  1. 暂无评论