Draw Polygon Using Ngmap
Solution 1:
3
As the example you referenced shows, the overlaycomplete
callback function receives e
(Event) as the first parameter. This OverlayCompleteEvent
object contains reference to the drawn overlay at Event.overlay
(see docs of the original Google Maps JS API more info, since ng-map
is only wrapper of the original API after all, specifically "OverlayCompleteEvent object specification" section).
Using that you can get the reference to the created overlay, store it and then later call .setMap(null)
on it to delete it from map. E.g:
$scope.myDrawnOverlays = []; //array where you store drawn overlays
$scope.onMapOverlayCompleted(e){
...
myDrawnOverlays.push(e.overlay); //store reference to the drawn overlay after it's drawn
}
$scope.deleteAllDrawnOverlays(){
for(var i = 0; i < myDrawnOverlays.length; i++){
myDrawnOverlays[i].setMap(null); //remove overlays from map one by one
}
myDrawnOverlays = [];
}
Then whenever you want to delete all overlays, just call the function $scope.deleteAllDrawnOverlays
(you can tie it to some button using ng-click
or do whatever you want with it).
4
This is NOT possible using google.maps.drawing.DrawingManager
which is used in your example. The reason is that DrawingManager doesn't support events for clicking at the map while drawing (see docs again for possible events, section "DrawingManager class").
So except hacky workarounds which could break any time with new api release, the only possible solution is to implement drawing of polygon yourself and not use DrawingManager
. Check for example this SO answer, specifically the referenced example (right-clicks on map). They don't use ng-map, but basically what would you have to do is to add on-click="mapClickHandler"
to your ng-map
, in the mapClickHandler
get position where you clicked on map, draw Marker there which would look like a dot and if you have more dots connect them with lines.
This is however too much for one question, you would have to probably read up on ng-map
and relevant sections of Google Maps Js Api yourself.
Post a Comment for "Draw Polygon Using Ngmap"