Add x y coordinates in web map layer

12-28-2016 01:06 PM
Status: Open
New Contributor III

I'd like to see an add x y coordinate tool for feature layers in AGO web maps.  X Y cords in lat long decimal degrees would be excellent.

Users using new Esri tools such as trek2there that requires lat and long fields from the attribute table as input would benefit, and AGO users wouldn't have to add x y cords in desktop only to republish a layer with x y cords.


So much yes. Why is this not included?!? Esri has fallen in love with WGS84 web mercator and punishes you for using anything else on the web. So at least give us the XY (lat long) coordinates!!!


Would you need the X/Y value to update dynamically if the point moved or would you be fine with having to run the tool again like how it is in ArcMap and Pro.


Arcade expressions can to some degree be used for this purpose. The following expression returns lat/long values, and can be used in popups and labels. Note it only works where the basemap is in WGS84 or Web Mercator Auxiliary Sphere.

//Convert Lines/Polygons to Points
var PointGeometry = Centroid(Geometry($feature));

var ArcadeX = PointGeometry.x;
var ArcadeY = PointGeometry.y;
var ArcadeSr = PointGeometry.spatialReference.wkid;
var Latitude, Longitude;

function AuxSphereToLatLon(x, y) {
    //Conversion based on
    var rMajor = 6378137;
    var shift = PI * rMajor;
    Longitude = x / shift * 180.0;
    Latitude = y / shift * 180.0;
    Latitude = 180 / PI * (2 * Atan(Exp(Latitude * PI / 180.0)) - PI / 2.0);

if (ArcadeSr == 4326) {
    Console("4326 Spatial Reference - No Conversion Necessary");
     Latitude = ArcadeY;
     Longitude = ArcadeX;
} else if (ArcadeSr == 102100) {
    Console("102100 Spatial Reference - Conversion Necessary");
    AuxSphereToLatLon(ArcadeX, ArcadeY);
} else {
    Console(ArcadeSr + " Spatial Reference is not supported - currently works with Web Maps where the basemap is in WGS84 (4326) or Web Mercator Auxiliary Sphere 102100");

function ReturnLatLong(Lat, Long, Decimals) {
     return Text(Round(Lat, Decimals)) + ", " + Text(Round(Long, Decimals));

ReturnLatLong(Latitude, Longitude, 7);

A similar expression can be used to build hyperlinks as in this example on GitHub which hyperlinks to the same location in Google Maps. A simlar expression could be used for Trek2There.

Arcade is not supported in Collector or Explorer popups just yet, but this is coming down the line and the above expression will work when this becomes supported. Survey123 can currently hyperlink to Trek2There by building up a hyperlink using the URL Scheme.



I've been trying to do this exact thing - return the Lat/Long from a point in a web map using ArcGIS JavaScript. In the code you have here and the code I've tried I keep getting an error "Execution Error:Runtime Error: Cannot call member method on null. x".  I'm not sure what I need to do to fix this. I can get the geometry values as a json structure, but I cannot get to the individual data elements. 

Any suggestions?


If you can see the geometry values as a json structure, then it sounds like it's just a syntax problem. Can you post your code on jsbin or similar?


I'd not used jsbin before, thanks for the suggestion.  However, I wasn't able to resolve that as it looks like there are many more references I need to include to build that code. In AGO, I'm simply doing:

Expression labeled Lat -

var myY = Geometry($feature),y;
return (myY)

Expression labeled Lon -

var myX = Geometry($feature),longitude;
return Text(myX);

When I click on the point in AGO, the popup gives me:

Lat: [object Object]
Lon: {"x":-9253301,"y":4883077,"spatialReference":{"latestWkid":3857,"wkid":102100}}

I tried to do a return on myX.x (x value for Lon function), but I get the error "Cannot call member method on null. x "


There is a syntax error here: var myY = Geometry($feature),y;

The comma needs to be changed to a full stop: var myY = Geometry($feature).y;

The XY values returned by Arcade are based on the basemap. So to get the Lat/Lon where the basemap is in Web Mercotor Auxiliary Sphere you need to use the AuxSphereToLatLon function I have in the script I listed above.

Here is a webmap which contains the code I have above - you may have to 'Save As' the Web Map to see the popup configuration.


I tried the hard stop, and I'm getting an error. Is there something I'm supposed to be including in my overall map or json that may be causing this?  

Today I copied the code from your example, top to bottom, created a new custom command, pasted the code , ran the test, and received the same error message: "Execution Error:Runtime Error: Cannot call member method on null. x"  The error is occurring at var ArcadeX = Geometry($feature).x;

By the way - I really do appreciate all the assistance.


I've reached out to Esri's technical support for assistance.


It would be amazing if the X/Y updated dynamically in AGOL. reason being: in my field of work, I have created a user friendly web map for my analysts to place points on. If i create fields of Lat Long for them to fill out, they can easily type one coordinate and place their point somewhere else.

I would really like to see the fields dynamically populate as one places the points. As it stands, I see it as much safer to not even add a Lat Long field set, as it can very confusing if the hand entered coords are 12345N 123456W, but the point is placed on a map at 24580N 135790W