简体   繁体   中英

Remove unfinished polygons/polylines in Google Maps API v3

So my problem is: I'm trying to find a way to clear a drawn polygon while it is still being drawn. Even when I create a new google.maps.Map object I still can't get rid of the old polygon or polyline such as doing this:

光标外图

Currently, I store all drawn polygons/polylines in an array and delete them when needed by calling the array element. The only problem is I can't delete a polygon/polyline unless it is in the array. Here's the relevant code:

//Code to reset map
bounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(latSE, lonNW),
    new google.maps.LatLng(latNW, lonSE));

var mapOptions = {
    center: new google.maps.LatLng((latNW + latSE) / 2,
        (lonNW + lonSE) / 2),
    zoom: 12,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
};
map = new google.maps.Map(document.getElementById("map-canvas"),
    mapOptions);
map.fitBounds(bounds);
drawingManager.setMap(map);
deleteAllRegions(); //remove all regions after switching

And the delete regions function:

//delete all completed polygons
function deleteAllRegions() {
    for (var j = 0; j < regions.length; j++) {
        regions[j].setMap(null)
    }
    regions = new Array();
    for (var j = 0; j < paths.length; j++) {
        paths[j].setMap(null)
    }
    paths = new Array();
}

It's less difficult,

  1. call drawingManager.setDrawingMode(null);

    This will finish the current action, eg when you currently draw a polygon the polygon will be completed and the overlay_complete -event fires (which should push the shape into the array)

  2. run the code that clears the shapes(the last shape will also be inside the array)

No need to re-create map or drawingManager

Just found out I wasn't deleting the drawingManager when clearing. The new code is:

//Code to reset map
bounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(latSE, lonNW),
    new google.maps.LatLng(latNW, lonSE));

var mapOptions = {
    center: new google.maps.LatLng((latNW + latSE) / 2,
        (lonNW + lonSE) / 2),
    zoom: 12,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
};
//create new drawing manager
drawingManager = new google.maps.drawing.DrawingManager({
    drawingControlOptions: {
        drawingModes: [
            google.maps.drawing.OverlayType.POLYLINE,
            google.maps.drawing.OverlayType.POLYGON
        ]
    },
    polylineOptions:{
        editable: true
    },
    polygonOptions:{
        editable: true
    }
});
//create new map
map = new google.maps.Map(document.getElementById("map-canvas"),
    mapOptions);
map.fitBounds(bounds);
drawingManager.setMap(map);
deleteAllRegions(); //remove all regions after switching

It seems since the drawing manager was the same, it would keep the previously drawn polygons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM