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

2

17

I am trying to get the updated polygon paths when the polygon is edited and dragged. I tried to do it like the code below.

In my Typescript:

 @ViewChild(AgmPolygon) polygon: any;
 this.polygon.getPaths().then((x: any[]) => {
   x.forEach(y => {
     console.log('-');
     y.forEach(z => console.log(z.lat(), z.lng()));
   });
 });

I followed the following links also 1st link 2nd link.

In my html:

<agm-map [latitude]="lat" [fullscreenControl]="true" 
    (mapClick)="mapClicked($event)" [longitude]="lng" [zoom]="8" [mapTypeControl]="true">

  <agm-polygon [fillColor]="item.fillColor" (polyMouseOut)="polyMouseOut($event)" 
    (polyMouseMove)="polyMouseMove($event)" [fillOpacity]="item.fillOpacity" 
    *ngFor="let item of zonesPath; let i=index" [editable]="item.ZoneID==ZoneID" 
    (polyMouseUp)="polyMouseUp(item,$event)" (polyMouseDown)="polyMouseDown($event)" 
    (polyDblClick)="polyDblClick($event)" (polyDragStart)="polyDragStart($event)" 
    (polyDragEnd)="polyDragEnd($event,item)" (polyDrag)="polyDrag($event)" 
    [polyDraggable]="false" [paths]="item.ZonePaths" (polyClick)="polyclick($event)">
  </agm-polygon>

  <agm-polygon [paths]="paths"></agm-polygon>
</agm-map>

i am doing *ngFor for polygon.and my json data is:

{
"ZoneID": "594dff0ee10452d8567b23c6",
"strokeColor" : "#038063",
"strokeOpacity" : 0.2,
"strokeWeight" : 2,
"fillColor" : "#038063",
"fillOpacity" : 0.35,
"draggable" : false,
"editable" : false,
"visible" : true,
"ZonePaths": [ 
    {
        "lat" : 17.535107299215,
        "lng" : 78.2346725463869
    }, 
    {
        "lat" : 17.541981926489,
        "lng" : 78.240509033203
    }, 
    {
        "lat" : 17.54460076354,
        "lng" : 78.249778747559
    }, 
    {
        "lat" : 17.55082034986,
        "lng" : 78.284454345703
    }]}
Stancil answered 6/11, 2017 at 6:40 Comment(4)
i am getting error like this.polygon.getPaths is not a functionStancil
Can you please provide your data too? I mean, in the template you reference some fields, like items or etc, I try to reproduce this locally and just want to make sure I do it as isDutton
I would suggest you create a plunkr or stackblitz.Isomerism
I am working on similar kind of requirement. It would be great if you could post github link for the above code. I am trying to load mutiple polygon from database.Fencer
M
3

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())
Manamanacle answered 12/6, 2018 at 14:21 Comment(0)
R
1

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);
}
Rafaelarafaelia answered 12/3, 2019 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.