1

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

3

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.

5
  • 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
    Commented Feb 28, 2019 at 9:06
  • That isn't relevant, a featurelayer is based on features, which are of type graphic.
    – Berend
    Commented 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
    Commented 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 hint
    – Joy
    Commented 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. Commented Mar 16, 2019 at 18:16
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.