Understanding Googlemaps Polygon Set_at Events

Asked 2 years ago, Updated 2 years ago, 67 views

Unable to retrieve polygon deformation events with multiple paths.

// Creating polygons
var path1 = [ // Outer polygon]
    new google.maps.LatLng (36.66841891894785, 138.636474609375),
    new google.maps.LatLng (36.90597988519295, 139.3780517578125),
    new google.maps.LatLng (36.756490329505176, 139.7076416015625),
    new google.maps.LatLng (36.38149043210595, 140.0701904296875),
    new google.maps.LatLng (35.92019610057511, 140.020751953125),
    new google.maps.LatLng (35.60371874069731, 139.59228515625),
];
var path2 = [// inner polygon]
    new google.maps.LatLng (36.421282443649496, 139.1967734375),
    new google.maps.LatLng (36.06686213257888, 138.8671875),
    new google.maps.LatLng (35.817813158696616, 139.1748046875),
    new google.maps.LatLng (35.71083783530009,139.98779296875)
];
varpolygonOptions={
    paths — [path1, path2],
    geodesic —true,
    editable: true,
    clickable —true
};
var polygon=new google.maps.Polygon(polygonOptions);

// deformation event acquisition
varpolygonPaths=polygon.getPaths();
for (vari=0;i<=polygonPaths.length-1;i++){
    google.maps.event.addListener(value, 'set_at', function(){
           console.log("aa");
      });
}

If you grant an event to a path obtained by polygon.getPath(),
I was able to catch the transformation event of the outer polygon.

javascript google-maps

2022-09-30 21:17

1 Answers

There is no definition of value in the code you provided, but it probably fails to get this value.

The Polygon.prototype.getPaths() return value is the MVCArray object.
Because it is not just an array, you cannot access it with subscripts like polygonPaths[i].
Call the MVCArray.prototype.getAt() method in the for statement or use the MVCArray.prototype.forEach() method.

var polygonPaths=polygon.getPaths();
for (vari=0;i<polygonPaths.length;i++) {
  varpolygonPath=polygonPaths.getAt(i);
  google.maps.event.addListener(polygonPath, 'set_at', function(){
    console.log("aa");
  });

  /* You can also write as follows.
  polygonPath.addListener('set_at', function(){
    console.log("aa");
  });
  */
}

Or this way

var polygonPaths=polygon.getPaths();
polygonPaths.forEach(function(polygonPath){
  google.maps.event.addListener(polygonPath, 'set_at', function(){
    console.log("aa");
  });
});


2022-09-30 21:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.