Greg Thompson
Greg Thompson

Reputation: 894

Limit Google Maps Drawing library to one Polyline/shape

I have somewhat of a unique problem. I'd like to allow a user to draw only one polyline and limit it to that. I'd also like that to be able to edit the polyline at will. I found this example: Google Maps Drawing Manager limit to 1 polygon but that basically just disables the control panel which still doesn't limit anything and secondly I want them to be able to edit their polyline, just not be able to create new ones.

I'd even be fine if they had some sort of a onDrawStart listener that would clear the previous shape but as far as I can tell, they don't have any such listener. Thanks!

Upvotes: 2

Views: 2550

Answers (1)

Esselans
Esselans

Reputation: 1546

Add a listener to overlaycomplete or polylinecomplete. Save the new layer. Then you can either delete the old one after the user draw another one..

google.maps.event.addListener(map.drawingManager, "overlaycomplete", function(event){                                       
        event.overlay.overlayType = event.type;
    lastOverlay = event.overlay; // Save it
    map.drawingManager.setDrawingMode(null); // Return to 'hand' mode 
});  

So, if you want to delete the layer => lastOverlay.setMap(null).

There is also a listener that fires when the user select a drawing tool. Mmm I can't find this listener. Not sure if it really exists.

Upvotes: 1

Related Questions