如何使用绘图管理器从谷歌地图中删除绘制的圆或多边形 - ng2-map

3

如何使用绘图管理器从谷歌地图中删除绘制的圆形或多边形。

组件:

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>

但是在多边形完成函数或圆形完成时,我无法从地图对象中获取绘制的多边形。


请参考此链接,它一定会对您有所帮助。 点击 - user7771871
请参考此链接,它肯定会对您有所帮助。 点击这里 - user7771871
1个回答

2
您可以从CircleComplete/PolygonCompolete事件的参数中找到绘制的多边形或圆形。或者通过event.overlay从OverlayComplete事件的参数中找到目标对象。获取目标对象后,您可以将其保存在其他地方以便删除。
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);

以下是关于OverlayComplete事件的GoogleMapApi文档:

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,供您参考。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接