How can I get a graphic for feature without a map?

139
6
Jump to solution
10-14-2019 02:51 PM
MattStayner
New Contributor III

When I select a feature on a map and use popup.getSelectedFeature() I get a great JSON object that has all kinds of information about the feature, including: feature attributes, the layer the feature is on, etc. That's great! Is it possible to get the same information when I don't have a map loaded?

I have a page in my application where I don't have a map at all. I just show information from my own custom database that relates to features. I want to be able to open the same custom info panel that I created for use on my map page on this other page. To do that, I need the following information for the feature:

  • feature._layer.id
  • feature.geometry
  • feature.geometry.type
  • feature.attributes
  • feature.getLayer().applyEdits

I looked into using the ArcGIS REST API query, but it doesn't give me any information about the layer or the full geometry object.

I hoping there is a way to select a feature using its ObjectID without a map. Thanks!

Reply
0 Kudos
1 Solution

Accepted Solutions
BenElan
Occasional Contributor II

You can create a graphic without having a map. Here is a sample: https://codepen.io/benesri/pen/XWWKbwK?editors=1000 

View solution in original post

Reply
0 Kudos
6 Replies
Noah-Sager
Esri Regular Contributor

I'm pretty sure you can get all the information you want via the Query operation against the REST endpoint. Here I just supplied the OBJECTID and set the outfields to * and returnGeometry to true. For example:

https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/2/query?where=&text=&objec...

We also have a similar sample that uses the FindTask:

https://developers.arcgis.com/javascript/latest/sample-code/sandbox/index.html?sample=tasks-find

BenElan
Occasional Contributor II

Just as a side note, if you want it to be a json you can replace the 'f=html' at the end of the url with 'f=pjson' like so:

https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/2/query?where=&text=&objec...

Here is a sample I created that queries the rest endpoint in JavaScript: https://codepen.io/benesri/pen/oNNgGXP 

I add it to a map, but you can just as easily access the json object instead.

MattStayner
New Contributor III

Thanks to both of you for your responses. Yes, you can get some information about feature using the REST API, but not nearly as much as you can by selecting the graphic on the map. For example, you can see below, the geometry from the REST Endpoint contains X and Y. However, from the Graphic there is much more data including hasM, hasZ, latitude, longitude, spatialReference, type, X and Y. It's the same things with layer. I was hoping there was an easy way to get around it, but it sounds like I will still have to go ahead a create a layer and select a feature to get all the same data, even if there isn't a map loaded. Is that right?

Reply
0 Kudos
BenElan
Occasional Contributor II

You can create a graphic without having a map. Here is a sample: https://codepen.io/benesri/pen/XWWKbwK?editors=1000 

View solution in original post

Reply
0 Kudos
Noah-Sager
Esri Regular Contributor

I believe that you can still get all the relevant information from REST. If it's a feature that is hosted somewhere, all the pertinent information is there, it's really just a questions of formatting. For example, the hasM or hasZ is false for both in the image above. This means that there is no M-value or Z-value, so those values will not be returned from the REST endpoint (they would be returned if they existed). The x and y values are just converted lat/longs (or vice versa). The type is available from REST, just in a different format, e.g.:

"geometryType": "esriGeometryPolygon",

Lastly, the spatial reference is available on the service itself.

MattStayner
New Contributor III

Hi Ben and Noah,

Both great points. Noah, yes it's true, I'm sure it is all there using the REST API, it just would take a little bit of work. Ben, that is what I was looking for. The answer seems obvious now that you point it out. I don't need a map and layers and all that, just a quick query and create a graphic. Perfect! Thanks again!