How to add a graphic with geojson string?

300
4
03-28-2020 10:36 AM
Highlighted
New Contributor

Hi

I'm using Arcgis API for JS 4.14,it provides GeoJSONLayer to add geoJSON data from url, just like this:

var geoJsonLayer1 = new GeoJsonLayer({ url: "http://www.myCorsEnabledServer.com/canada.json" });

However, my data and geo information is stored in MS SQL server,ever single record has a GeoJSON string, like {"type":"Point","coordinates":[119.84039205155467,32.309760149806948]}, I can't load it directly by GeoJSONLayer because there are no urls.

I think I can read the GeoJSON strings and creat graphics for each record, but how? Is there any way to convert GeoJSON string to graphic?

 

Reply
0 Kudos
4 Replies
Highlighted
Frequent Contributor

You can load is as URL object via Blob.

  const blob = new Blob([JSON.stringify(geojson)], {
type: "application/json"
});
const url = URL.createObjectURL(blob);
const layer = new GeoJSONLayer({ url });

Demo: https://codepen.io/odoe/pen/YzKqLZO 

Highlighted
MVP Esteemed Contributor

Rene,

  Sure would be nice to just have a string constructor option too.

Reply
0 Kudos
Highlighted
Frequent Contributor

It's on the roadmap, not sure when it will be done though. I'll twist some screws, ha!

Reply
0 Kudos
Highlighted
Esri Contributor

Hi Yiyuan,

There does not appear to be any direct way to create a Graphic from a GeoJSON string.  The Graphic class in the ArcGIS API for JavaScript does have a fromJSON() method but, the JSON format that it is expecting is not quite the same as GeoJSON.  The fromJSON() method is expecting the coordinates to look like the format below:

geometry: {
      x: -49.97,
      y: 41.73
}

You can suggest the ability to create Graphics from GeoJSON strings on the Esri Ideas page.  I searched to see if this feature had already been suggested but it appears it has not.  Check out the links under Getting Started at the right of the Ideas page to learn how to submit an idea.

Sincerely,

Chris

Reply
0 Kudos