How to get paths of agm polygon when it is edited and dragged?

有些话、适合烂在心里 提交于 2019-12-04 23:11:11

I had to overcome this after reading ATM agm polygon edit change event it still on PR.

So, I return to basics and look for google map api class v3.

@ViewChild(AgmMap) map: any;
polygon: any;
this.map._mapsWrapper.createPolygon({
      paths: this.area,
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.3,
      editable: true,
    }).then((polygon: any) => {
      this.polygon = polygon
    });

After this, you can add event or get the path changes with getPath()

console.debug(this.polygon.getPath())

First get the map:

HTML file:

<agm-map (mapReady)="onMapReady($event)">

TS file:

@ViewChild(AgmMap) map: any
map: any;

onMapReady(map){
  this.map = map;
}

Then create the polygon object and add it to the map:

TS file:

    // Define the LatLng coordinates for the polygon's path.
const triangleCoords = [
  { lat: 25.774, lng: -80.190 },
  { lat: 18.466, lng: -66.118 },
  { lat: 32.321, lng: -64.757 },
  { lat: 25.774, lng: -80.190 }
];


// Construct the polygon.
this.polygon = new google.maps.Polygon({
  paths: triangleCoords,
  strokeColor: '#FF0000',
  strokeOpacity: 0.8,
  strokeWeight: 2,
  fillColor: '#FF0000',
  fillOpacity: 0.35,
  editable: true,
  draggable: true,
});

//Set polygon to map
this.polygon.setMap(this.map);

Read paths:

this.getPolygonCoordinates(this.polygon);

...

getPolygonCoordinates(draggablePolygon) {
  const len = draggablePolygon.getPath().getLength();
  const polyArrayLatLng = [];

  for (let i = 0; i < len; i++) {
    const vertex = draggablePolygon.getPath().getAt(i);
    const vertexLatLng = { lat: vertex.lat(), lng: vertex.lng() };
    polyArrayLatLng.push(vertexLatLng);
  }

  console.log(polyArrayLatLng);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!