How to remove a drawn circle or polygon from a Google map using the drawing manager - ng2-map

How to remove a drawn circle or polygon using the graphical manager from a google map.

component:

import {Ng2MapComponent, DrawingManager, Polygon} from 'ng2-map';

export class CreateAlertComponent implements OnInit {
   @ViewChild(Ng2MapComponent) mapObj: Ng2MapComponent;
   @ViewChild(DrawingManager) drawingManager: DrawingManager;

   polygonCompleteFunction(e) {
       console.log(this.mapObj);
   };

});

HTML:

<ng2-map [zoom]="mapOptions.zoom" [minZoom]="mapOptions.minZoom" [center]="mapOptions.center" clickable="false" (click)="mapClick($event)">
                    <drawing-manager *ngIf = "selectedJurisdictions.length > 0" 
                        [drawingMode]="'null'"
                        [drawingControl]="true"
                        [drawingControlOptions]="{
                        position: 2,
                        drawingModes: ['circle', 'polygon']
                        }"
                        [circleOptions]="{
                        fillColor: 'red',
                        fillOpacity: 0.3,
                        strokeColor: 'black',
                        strokeWeight: 2,
                        editable: true,
                        draggable: true,
                        zIndex: 1
                        }"
                        [polygonOptions]="{
                        fillColor: 'red',
                        fillOpacity: 0.3,
                        strokeColor: 'black',
                        strokeWeight: 2,
                        editable: true,
                        draggable: true,
                        zIndex: 1
                        }"
                        (polygoncomplete)="polygonCompleteFunction($event)"
                        (circlecomplete)="circleCompleteFunction($event)">
                    </drawing-manager>
</ng2-map>

But when I complete a full or polygonal polygon, I don’t get the drawn polygons from the map object

+4
source share
1 answer

You can find a drawn polygon or circle from the CircleComplete/ PolygonCompoleteEvent parameter . Or find the target from the OverlayComplete event.overlay event parameter. After receiving the target, you can save it somewhere to delete them elsewhere.

polygonCompleteFunction(e) {
   console.log(e);    // this is the drawn Polygon you are looking for, and same for the circleComplete event
};

overlayComplete(e) {
  console.log(e.overlay);  // here can also find the drawn shape(Polygon/Circle/Polyline/Rectangle)
}

, .

target.setMap(null);

GooleMapApi OverlayComplete:

google.maps.event.addListener(drawingManager, 'circlecomplete', function (circle)   {     var radius = circle.getRadius();   });

google.maps.event.addListener(drawingManager, 'overlaycomplete', function (event)   {     if (event.type == 'circle') {       var radius = event.overlay.getRadius();     }   });

GoogleMapApi.

, . plunker .

+1

Source: https://habr.com/ru/post/1672449/


All Articles