I am currently working with the the ArcGIS API for JavaScript and I have delved into ArcGIS API for JavaScript currently for a weeks, so I am not that familiar with the ArcGIS API for JavaScript.
For now I managed to create a webapplication with a nice Map and a layerlist, so I managed to learn about the basic widgets of ArcGIS API.
However I included code in which whenever I click on an object of the layerlist it will show an popup and execute a certain function. For my function, I would like to have the location of the selected feature in coordinates (presumably in WGS84 coordinates). I was therefore wondering how I would be able to retrieve the coordinates of the selected feature, in the attributetable of the feature, there are no longitude and latitude columns for example, so I was wondering in which other way I would be able to retrieve the coordinates of the selected feature and being able to store it into a variable. (Whether there is a widget for that for example)
I currently use the JavaScript API 4.10 version. EDIT: The layer is a feature layer.
2 Answers 2
If you have a graphic object, you can inspect it's geometry property. This in turn has a type property which tells you the type of geometry (Point, Polyline, etc)
If geometry.type === "point"
you know it's a point, and you can get the x
and y
properties. For other geometry types you would need additional logic, e.g. for a polygon you could get the centroid, and for a polyline you could calculate it's mid point.
-
Hi Berend, thank for the explanation, unfortunately my layer is a feature layer, I forgot to mention it. But thanks for explaining in case when the object was graphic object.Joy– Joy2019年02月28日 09:06:38 +00:00Commented Feb 28, 2019 at 9:06
-
That isn't relevant, a featurelayer is based on features, which are of type
graphic
.Berend– Berend2019年02月28日 09:17:59 +00:00Commented Feb 28, 2019 at 9:17 -
I'm not sure I understand that last comment ('Do you know by any change how I can use the graph object in the Polygon function'), could you explain what you want exactly? Or even better, post a new question?Berend– Berend2019年02月28日 12:23:21 +00:00Commented Feb 28, 2019 at 12:23
-
@ Berend sorry for the confusion, my question was not that well formulated, what I meant to say is whether I could apply the properties of the polygon function to the graphic object and I managed to do that with the hint you gave (geometry.type) and instead I used geometry.centroid.latitude. Thank you very much for the hintJoy– Joy2019年02月28日 12:28:01 +00:00Commented Feb 28, 2019 at 12:28
-
@Joy, if Berend's answer helped you find the solution, it means it was useful. You can upvote it. See: gis.stackexchange.com/help/someone-answers. Tks.Andre Silva– Andre Silva2019年03月16日 18:16:08 +00:00Commented Mar 16, 2019 at 18:16
With the help of @Berend, I managed get the coordinates of the selected feature by writing the following code.
var latitude=result.graphic.geometry.centroid.latitude;
var longitude=result.graphic.geometry.centroid.longitude;
Therefore after the graphic object using the geometry, centroid and the longitude or latitude properties will results in the value of the longitude and the latitude.