Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
    }]}
like image 415
paruvelly Vishwanath Avatar asked Nov 06 '17 06:11

paruvelly Vishwanath


2 Answers

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())
like image 130
David L Avatar answered Nov 12 '22 22:11

David L


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);
}
like image 34
Flavio Marques Avatar answered Nov 12 '22 23:11

Flavio Marques