Creating a featurelayer from an array of locations

331
1
01-13-2022 12:26 PM
BushraSyed1
New Contributor II

Hello!

I was wondering if it would be possible to turn a 2d array of latitude and longitude into a featurelayer? Would this be difficult to accomplish? This is what the data looks like:

var data = [
        { "latitude": 59.441193, "longitude": 24.729494 },
        { "latitude": 59.432365, "longitude": 24.742992 },
        { "latitude": 59.431602, "longitude": 24.757563 },
        { "latitude": 59.437843, "longitude": 24.765759 },
        { "latitude": 59.439644, "longitude": 24.779041 },
        { "latitude": 59.434776, "longitude": 24.756681 }
    ];
Tags (3)
0 Kudos
1 Reply
ReneRubalcava
Frequent Contributor II

You can iterate over the array and add them to the source of the FeatureLayer.

const data = [
  { latitude: 59.441193, longitude: 24.729494 },
  { latitude: 59.432365, longitude: 24.742992 },
  { latitude: 59.431602, longitude: 24.757563 },
  { latitude: 59.437843, longitude: 24.765759 },
  { latitude: 59.439644, longitude: 24.779041 },
  { latitude: 59.434776, longitude: 24.756681 },
];

const featureLayer = new FeatureLayer({
  geometryType: "point",
  objectIdField: "OBJECTID",
  source: data.map((a) => ({
    geometry: {
      type: "point",
      ...a,
    },
  })),
});
0 Kudos