Adding Lat/Lon to ArcGIS Pop-ups

Matt Gaffner
4 min readMay 6, 2021

--

Many of the point data services that we work with and provide to our customers at DTN don’t have an explicit field for the latitude or longitude. Instead, it’s provided in the geometry. For this reason, it doesn’t show up in the pop-ups, but for many it’s still important to see this information. Fortunately, you can add a custom expression to display the lat/lon data to the pop-up with a quick copy & paste and a few mouse clicks. The data service that customers ask about seeing the lat/lon in the pop-up the most is lightning, so I’ve used that as my example.

(Note: The service used in this example is in EPSG 4326. I have not tried this with other spatial references)

DTN lightning data showing pop-up in ArcGIS
Default ArcGIS pop-up showing all of the attributes.

Step 1 — For the point layer of interest (lightning for me), click “Configure Pop-up”

For your point layer, choose “Configure Pop-up”

Step 2 — In the Configure Pop-up dialog, click “ADD” under Attribute Expressions

Click “ADD”

Step 3 — Paste code snippet from below into Custom Attribute Expression dialog box and click “Test” to see the results and confirm they work. If you want, change the default name of the expression from “Custom” to whatever you want. I went with “Lat/Lon.”

function MetersToLatLon(x, y) {
var originShift = 2.0 * PI * 6378137.0 / 2.0;
var lon = (x / originShift) * 180.0;
var lat = (y / originShift) * 180.0;
lat = 180.0 / PI * (2.0 * Atan( Exp( lat * PI / 180.0)) — PI / 2.0);
return [lat, lon];
}

var latlon = MetersToLatLon(Geometry($feature).X, Geometry($feature).Y);

var lat = round(latlon[0],5);
var lon = round(latlon[1],5);

return “Lat: “ + lat + “ , Lon: “ + lon;

Expression editor

Step 4 — Click “OK” a bunch to get back to the map.

Step 5 — On the map, click on one of your points to see the new pop-up with the latitude and longitude included:

Pop-up with Lat/Lon data from Custom Expression.

If you don’t like the latitude and longitude in a single field, you can use the snippets below to create a separate expressions for latitude and longitude. Make sure you name the custom expression appropriately.

Latitude:

function MetersToLatLon(x, y) {
var originShift = 2.0 * PI * 6378137.0 / 2.0;
var lon = (x / originShift) * 180.0;
var lat = (y / originShift) * 180.0;
lat = 180.0 / PI * (2.0 * Atan( Exp( lat * PI / 180.0)) — PI / 2.0);
return [lat, lon];
}

var latlon = MetersToLatLon(Geometry($feature).X, Geometry($feature).Y);

var lat = round(latlon[0],5);
var Lon = round(latlon[1],5);

return lat;

Longitude:

function MetersToLatLon(x, y) {
var originShift = 2.0 * PI * 6378137.0 / 2.0;
var lon = (x / originShift) * 180.0;
var lat = (y / originShift) * 180.0;
lat = 180.0 / PI * (2.0 * Atan( Exp( lat * PI / 180.0)) — PI / 2.0);
return [lat, lon];
}

var latlon = MetersToLatLon(Geometry($feature).X, Geometry($feature).Y);

var lat = round(latlon[0],5);
var lon = round(latlon[1],5);

return lon;

Note, no matter how many decimal places you specify in the expression, if the latitude and longitude are simple attribute expressions, then the default will be 2 decimal places. It’s a little annoying, but a quick fix:

Lat and Lon expressions only show up with 2 decimal places.

Under “Configure Pop-up”, choose “Configure Attributes”:

Click “Configure Attributes”

Choose the Lat (or Lon) expression and change the Format to 5 decimal places or (however many you’d like to display):

Change the attribute decimal places

--

--

Matt Gaffner
Matt Gaffner

Written by Matt Gaffner

Weather Nerd. GIS Geek. Analyzing all things spatiotemporal. DTN Weather — matt.gaffner {@}dtn.com

No responses yet