How to add a graphic with geojson string?

2018
4
03-28-2020 10:36 AM
YiyuanGao
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?

 

0 Kudos
4 Replies
ReneRubalcava
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 

RobertScheitlin__GISP
MVP Emeritus

Rene,

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

0 Kudos
ReneRubalcava
Frequent Contributor

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

0 Kudos
ChristopherKoenig
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

0 Kudos