Array contains latitude and longitude of a polygon in Google maps drawingManager

后端 未结 2 1162
不知归路
不知归路 2021-01-03 12:06

This is the most simplest way to add a drawing manager on the Google maps to draw polygon, circle, rectangle and etc by user.

code:


<         


        
2条回答
  •  花落未央
    2021-01-03 12:36

    Create a global array which will contains your polygons

    var polygons = [];
    

    Then, fill your array in polygoncomplete event :

    google.maps.event.addDomListener(drawingManager, 'polygoncomplete', function(polygon) {
        polygons.push(polygon);
    

    In order to let the users the possibility to edit polgons after creation, you have to call setEditable at the end of your polygonComplete event

    polygon.setEditable(true);
    

    if you need to read all the lat/lng of polygon's vertices, you can use this sample of code :

    var polygonBounds = polygon.getPath();
    var coordinates = [];
    
    for(var i = 0 ; i < polygonBounds.length ; i++)
    {
        coordinates.push(polygonBounds.getAt(i).lat(), polygonBounds.getAt(i).lng());
    } 
    

提交回复
热议问题