1

I have a leaflet map that uses esri-leaflet to display a feature layer created for arcgis that is a number of paths. These paths are created correctly and I can see them on the map, but they aren't the visual I need. Instead I'm interested in getting the LatLng of the centroid of the path.

Since I know leaflet polylines have a getCenter method, I just need to know how to access these objects after they are drawn to the map. Is this possible?

Code for this follows:

let mymap = L.map("mapid").setView([lat, lng], 18);
L.esri.basemapLayer("Gray").addTo(mymap);
var subtleUA = L.esri
  .tiledMapLayer({
    url:
      "https://mymapserver.../"
  })
  .addTo(mymap);
let roomFeatures = L.esri.featureLayer({url:"https://mymapserver/../interiors/16"}).addTo(mymap)
1

After feature layer is loaded you can use layer's .eachFeature method to loop through features and get center of each feature:

let roomFeatures = L.esri.featureLayer({url:"https://mymapserver/../interiors/16"});

roomFeatures.on('load', function() {
  roomFeatures.eachFeature(function(layer) {
    let center = layer.getCenter();
    console.log(center);
  });
});
roomFeatures.addTo(mymap);
| improve this answer | |
  • excellent! I wasn't sure if the features were actual polylines after using esri leaflet but this is great! – Li Brary Jun 19 at 23:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.