1

Leaflet has a method built into the polyline class to return an array of all LatLng coordinates of the line. It would look something like:

var latlngArray = polyline.getLatLngs();

Esri-leaflet, the Esri plugin that allows for interaction with ArcGIS Online and ArcGIS REST services has a separate class--Layer--that, it seems, it uses for all hosted feature types--points, lines, polygons, etc. Used in Esri-leaflet, it's constructed with L.esri.featureLayer.

I'm essentially trying to take the latitude and longitude of the feature vertices and perform a custom transformation on them point by point before placing them on the map without altering their values on the server. By using the pointToLayer option I've been able to retrieve and manipulate the latlng values for points. This would be fairly simple if I could call .getLatLngs() on the feature layer.

Is there a way I can do something similar with a polyline?

1
  • It looks like the features are all L.geoJson objects, so you should be able to access the coordinates using each feature's .geometry.coordinates. See this answer, for example. Though it may require some reformatting of the nested arrays to get what you want, and I haven't checked it fully, here is an example based on one of the examples from the esri.featurelayer site: fiddle.jshell.net/nathansnider/8k7tLsf7 Commented Mar 31, 2016 at 21:03

1 Answer 1

1

Try something like this. Gives you an array of each polygons coordinates, but you could organize the x, y however you like once you access them.

          // add our feature layer to the map
          var dataLayer = L.esri.featureLayer({
             url: '/pathtoyourfeatureserver/FeatureServer/0'
          }).addTo(map);
        var coordsPoly = [];
        dataLayer.on('load', function () {
          dataLayer.eachFeature(function (layer) {
              var theseCoords = [];
              var thisPoly = layer._latlngs[0];
              var i = 0;
              while (i < thisPoly.length) {
                  var x = thisPoly[i].lng;
                  var y = thisPoly[i].lat;
                  theseCoords.push(x + ", " + y);
                  i += 1;
              }
              coordsPoly.push(theseCoords);
          });
          console.log(coordsPoly);
      });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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