Learn how to find an address near a location with the geocoding service .
Reverse geocoding is the process of converting a location to an address or place . To reverse geocode, you use the Geocoding service and the reverseGeocode
operation. This operation requires an initial location and returns an address with attributes such as place name and location.
In this tutorial, you use the reverseGeocode
method of ArcGIS REST JS to reverse geocode and find the closest address to your clicked location on the map.
PrerequisitesYou need a free ArcGIS developer account to access your dashboard and API keys . The API key must be scoped to access the services used in this tutorial.
Steps Create a new penTo get started, either complete the Display a map tutorial or use this pen . Set the API keyTo access ArcGIS location services , you need an API key .
Go to your dashboard to get an API key .
In CodePen , update apiKey
to use your key.
Change line
const apiKey = "YOUR-API-KEY" ;
const basemapId = "ArcGIS:Streets" ;
const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey;
olms(map, basemapURL);
Add referencesThis tutorial uses ArcGIS REST JS for reverse geocoding, and the ol-popup library to display pop-ups.
In the <head>
element, add references to the ArcGIS REST JS and ol-popup libraries.
Show more lines
Add line. Add line. Add line. Add line. Add line.
<!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" />
< meta name = "viewport" content = "initial-scale=1, maximum-scale=1, user-scalable=no" />
< title > OpenLayers Tutorials: Reverse Geocode </ title >
< style >
html ,
body ,
#map {
padding : 0 ;
margin : 0 ;
height : 100% ;
width : 100% ;
font-family : Arial, Helvetica, sans-serif;
font-size : 14px ;
color : #323232 ;
}
</ style >
< link rel = "stylesheet" href = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type = "text/css" />
< script src = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js" > </ script >
< script src = "https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type = "text/javascript" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-geocoding@3.0.0/dist/umd/geocoding.umd.js" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-auth@3.0.0/dist/umd/auth.umd.js" > </ script >
< script src = "https://unpkg.com/ol-popup@4.0.0" > </ script >
< link rel = "stylesheet" href = "https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
</ head >
< body >
< div id = "map" > </ div >
< script >
const apiKey = "YOUR-API-KEY" ;
const map = new ol.Map({
target : "map"
});
const view = new ol.View({
center : ol.proj.fromLonLat([ 2.3522 , 48.8566 ]), // Paris
zoom: 12
});
map.setView(view);
const popup = new Popup();
map.addOverlay(popup);
map.on( "click" , (e) => {
const coords = ol.proj.transform(e.coordinate, "EPSG:3857" , "EPSG:4326" );
const authentication = new arcgisRest.ApiKey({
key : apiKey
});
arcgisRest
.reverseGeocode(coords, {
authentication
})
.then( ( result ) => {
const message =
` ${result.address.LongLabel} <br>` + ` ${result.location.x.toLocaleString()} , ${result.location.y.toLocaleString()} ` ;
popup.show(e.coordinate, message);
})
.catch( ( error ) => {
popup.hide();
console .error(error);
});
});
const basemapId = "ArcGIS:Navigation" ;
olms.apply(map, "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey);
</ script >
</ body >
</ html >
Show more lines
Update the mapA navigation basemap layer is typically used in geocoding and routing applications. Update the basemap layer to use ArcGIS:Navigation
.
Update the basemap and the map initialization to center on location [2.3522,48.8566]
, Paris.
Show more lines
Change line Change line
<!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" />
< meta name = "viewport" content = "initial-scale=1, maximum-scale=1, user-scalable=no" />
< title > OpenLayers Tutorials: Reverse Geocode </ title >
< style >
html ,
body ,
#map {
padding : 0 ;
margin : 0 ;
height : 100% ;
width : 100% ;
font-family : Arial, Helvetica, sans-serif;
font-size : 14px ;
color : #323232 ;
}
</ style >
< link rel = "stylesheet" href = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type = "text/css" />
< script src = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js" > </ script >
< script src = "https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type = "text/javascript" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-geocoding@3.0.0/dist/umd/geocoding.umd.js" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-auth@3.0.0/dist/umd/auth.umd.js" > </ script >
< script src = "https://unpkg.com/ol-popup@4.0.0" > </ script >
< link rel = "stylesheet" href = "https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
</ head >
< body >
< div id = "map" > </ div >
< script >
const apiKey = "YOUR-API-KEY" ;
const map = new ol.Map({
target : "map"
});
const view = new ol.View({
center : ol.proj.fromLonLat([ 2.3522 , 48.8566 ]), // Paris
zoom: 12
});
map.setView(view);
const popup = new Popup();
map.addOverlay(popup);
map.on( "click" , (e) => {
const coords = ol.proj.transform(e.coordinate, "EPSG:3857" , "EPSG:4326" );
const authentication = new arcgisRest.ApiKey({
key : apiKey
});
arcgisRest
.reverseGeocode(coords, {
authentication
})
.then( ( result ) => {
const message =
` ${result.address.LongLabel} <br>` + ` ${result.location.x.toLocaleString()} , ${result.location.y.toLocaleString()} ` ;
popup.show(e.coordinate, message);
})
.catch( ( error ) => {
popup.hide();
console .error(error);
});
});
const basemapId = "ArcGIS:Navigation" ;
olms.apply(map, "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey);
</ script >
</ body >
</ html >
Show more lines
Add a pop-upCreate a Popup
to display the results of the reverse geocode. It is a type of Overlay
so you add it to the map with map.addOverlay
.
Create a Popup
and save it to a popup
variable. Add it to the map with map.addOverlay
.
Show more lines
Add line. Add line.
<!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" />
< meta name = "viewport" content = "initial-scale=1, maximum-scale=1, user-scalable=no" />
< title > OpenLayers Tutorials: Reverse Geocode </ title >
< style >
html ,
body ,
#map {
padding : 0 ;
margin : 0 ;
height : 100% ;
width : 100% ;
font-family : Arial, Helvetica, sans-serif;
font-size : 14px ;
color : #323232 ;
}
</ style >
< link rel = "stylesheet" href = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type = "text/css" />
< script src = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js" > </ script >
< script src = "https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type = "text/javascript" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-geocoding@3.0.0/dist/umd/geocoding.umd.js" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-auth@3.0.0/dist/umd/auth.umd.js" > </ script >
< script src = "https://unpkg.com/ol-popup@4.0.0" > </ script >
< link rel = "stylesheet" href = "https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
</ head >
< body >
< div id = "map" > </ div >
< script >
const apiKey = "YOUR-API-KEY" ;
const map = new ol.Map({
target : "map"
});
const view = new ol.View({
center : ol.proj.fromLonLat([ 2.3522 , 48.8566 ]), // Paris
zoom: 12
});
map.setView(view);
const popup = new Popup();
map.addOverlay(popup);
map.on( "click" , (e) => {
const coords = ol.proj.transform(e.coordinate, "EPSG:3857" , "EPSG:4326" );
const authentication = new arcgisRest.ApiKey({
key : apiKey
});
arcgisRest
.reverseGeocode(coords, {
authentication
})
.then( ( result ) => {
const message =
` ${result.address.LongLabel} <br>` + ` ${result.location.x.toLocaleString()} , ${result.location.y.toLocaleString()} ` ;
popup.show(e.coordinate, message);
})
.catch( ( error ) => {
popup.hide();
console .error(error);
});
});
const basemapId = "ArcGIS:Navigation" ;
olms.apply(map, "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey);
</ script >
</ body >
</ html >
Show more lines
Add click event handlerBefore you call the reverse geocoding service, you need the location of the clicked point. Use ol.proj.transform
to convert this into a latitude and longitude.
Add a click handler to the map. Inside, convert the clicked coordinates from the event object into latitude and longitude.
Show more lines
Add line. Add line. Add line. Add line. Add line.
<!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" />
< meta name = "viewport" content = "initial-scale=1, maximum-scale=1, user-scalable=no" />
< title > OpenLayers Tutorials: Reverse Geocode </ title >
< style >
html ,
body ,
#map {
padding : 0 ;
margin : 0 ;
height : 100% ;
width : 100% ;
font-family : Arial, Helvetica, sans-serif;
font-size : 14px ;
color : #323232 ;
}
</ style >
< link rel = "stylesheet" href = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type = "text/css" />
< script src = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js" > </ script >
< script src = "https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type = "text/javascript" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-geocoding@3.0.0/dist/umd/geocoding.umd.js" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-auth@3.0.0/dist/umd/auth.umd.js" > </ script >
< script src = "https://unpkg.com/ol-popup@4.0.0" > </ script >
< link rel = "stylesheet" href = "https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
</ head >
< body >
< div id = "map" > </ div >
< script >
const apiKey = "YOUR-API-KEY" ;
const map = new ol.Map({
target : "map"
});
const view = new ol.View({
center : ol.proj.fromLonLat([ 2.3522 , 48.8566 ]), // Paris
zoom: 12
});
map.setView(view);
const popup = new Popup();
map.addOverlay(popup);
map.on( "click" , (e) => {
const coords = ol.proj.transform(e.coordinate, "EPSG:3857" , "EPSG:4326" );
const authentication = new arcgisRest.ApiKey({
key : apiKey
});
arcgisRest
.reverseGeocode(coords, {
authentication
})
.then( ( result ) => {
const message =
` ${result.address.LongLabel} <br>` + ` ${result.location.x.toLocaleString()} , ${result.location.y.toLocaleString()} ` ;
popup.show(e.coordinate, message);
})
.catch( ( error ) => {
popup.hide();
console .error(error);
});
});
const basemapId = "ArcGIS:Navigation" ;
olms.apply(map, "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey);
</ script >
</ body >
</ html >
Show more lines
Call reverse geocode serviceUse the ArcGIS REST JS reverseGeocode
method to find an address closest to a point.
Inside the click handler, create a new arcgisRest.ApiKey
to access the geocoding service. Call the arcgisRest.reverseGeocode
method with the coordinates array.
Show more lines
Add line. Add line. Add line. Add line. Add line. Add line. Add line.
<!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" />
< meta name = "viewport" content = "initial-scale=1, maximum-scale=1, user-scalable=no" />
< title > OpenLayers Tutorials: Reverse Geocode </ title >
< style >
html ,
body ,
#map {
padding : 0 ;
margin : 0 ;
height : 100% ;
width : 100% ;
font-family : Arial, Helvetica, sans-serif;
font-size : 14px ;
color : #323232 ;
}
</ style >
< link rel = "stylesheet" href = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type = "text/css" />
< script src = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js" > </ script >
< script src = "https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type = "text/javascript" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-geocoding@3.0.0/dist/umd/geocoding.umd.js" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-auth@3.0.0/dist/umd/auth.umd.js" > </ script >
< script src = "https://unpkg.com/ol-popup@4.0.0" > </ script >
< link rel = "stylesheet" href = "https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
</ head >
< body >
< div id = "map" > </ div >
< script >
const apiKey = "YOUR-API-KEY" ;
const map = new ol.Map({
target : "map"
});
const view = new ol.View({
center : ol.proj.fromLonLat([ 2.3522 , 48.8566 ]), // Paris
zoom: 12
});
map.setView(view);
const popup = new Popup();
map.addOverlay(popup);
map.on( "click" , (e) => {
const coords = ol.proj.transform(e.coordinate, "EPSG:3857" , "EPSG:4326" );
const authentication = new arcgisRest.ApiKey({
key : apiKey
});
arcgisRest
.reverseGeocode(coords, {
authentication
})
.then( ( result ) => {
const message =
` ${result.address.LongLabel} <br>` + ` ${result.location.x.toLocaleString()} , ${result.location.y.toLocaleString()} ` ;
popup.show(e.coordinate, message);
})
.catch( ( error ) => {
popup.hide();
console .error(error);
});
});
const basemapId = "ArcGIS:Navigation" ;
olms.apply(map, "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey);
</ script >
</ body >
</ html >
Show more lines
Display the resultThe response from the reverseGeocode
operation contains two properties. address
is a structured object with fields such as street name and business name. location
contains the location of the returned address, which may differ from the coordinates you provided. Display these values in a pop-up. Use a catch
handler to check for errors accessing the service.
Add a then
handler. Use popup.show
to display the resulting address and coordinates.
Show more lines
Add line. Add line. Add line. Add line. Add line. Add line.
<!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" />
< meta name = "viewport" content = "initial-scale=1, maximum-scale=1, user-scalable=no" />
< title > OpenLayers Tutorials: Reverse Geocode </ title >
< style >
html ,
body ,
#map {
padding : 0 ;
margin : 0 ;
height : 100% ;
width : 100% ;
font-family : Arial, Helvetica, sans-serif;
font-size : 14px ;
color : #323232 ;
}
</ style >
< link rel = "stylesheet" href = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type = "text/css" />
< script src = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js" > </ script >
< script src = "https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type = "text/javascript" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-geocoding@3.0.0/dist/umd/geocoding.umd.js" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-auth@3.0.0/dist/umd/auth.umd.js" > </ script >
< script src = "https://unpkg.com/ol-popup@4.0.0" > </ script >
< link rel = "stylesheet" href = "https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
</ head >
< body >
< div id = "map" > </ div >
< script >
const apiKey = "YOUR-API-KEY" ;
const map = new ol.Map({
target : "map"
});
const view = new ol.View({
center : ol.proj.fromLonLat([ 2.3522 , 48.8566 ]), // Paris
zoom: 12
});
map.setView(view);
const popup = new Popup();
map.addOverlay(popup);
map.on( "click" , (e) => {
const coords = ol.proj.transform(e.coordinate, "EPSG:3857" , "EPSG:4326" );
const authentication = new arcgisRest.ApiKey({
key : apiKey
});
arcgisRest
.reverseGeocode(coords, {
authentication
})
.then( ( result ) => {
const message =
` ${result.address.LongLabel} <br>` + ` ${result.location.x.toLocaleString()} , ${result.location.y.toLocaleString()} ` ;
popup.show(e.coordinate, message);
})
.catch( ( error ) => {
popup.hide();
console .error(error);
});
});
const basemapId = "ArcGIS:Navigation" ;
olms.apply(map, "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey);
</ script >
</ body >
</ html >
Show more lines
Add a catch
handler. In most cases, errors occur when the user clicks in the water, so simply hide the pop-up when this happens. Use popup.hide()
to remove the pop-up and then write the error to the console.
Show more lines
Add line. Add line. Add line. Add line.
<!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" />
< meta name = "viewport" content = "initial-scale=1, maximum-scale=1, user-scalable=no" />
< title > OpenLayers Tutorials: Reverse Geocode </ title >
< style >
html ,
body ,
#map {
padding : 0 ;
margin : 0 ;
height : 100% ;
width : 100% ;
font-family : Arial, Helvetica, sans-serif;
font-size : 14px ;
color : #323232 ;
}
</ style >
< link rel = "stylesheet" href = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type = "text/css" />
< script src = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js" > </ script >
< script src = "https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type = "text/javascript" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-geocoding@3.0.0/dist/umd/geocoding.umd.js" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-auth@3.0.0/dist/umd/auth.umd.js" > </ script >
< script src = "https://unpkg.com/ol-popup@4.0.0" > </ script >
< link rel = "stylesheet" href = "https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
</ head >
< body >
< div id = "map" > </ div >
< script >
const apiKey = "YOUR-API-KEY" ;
const map = new ol.Map({
target : "map"
});
const view = new ol.View({
center : ol.proj.fromLonLat([ 2.3522 , 48.8566 ]), // Paris
zoom: 12
});
map.setView(view);
const popup = new Popup();
map.addOverlay(popup);
map.on( "click" , (e) => {
const coords = ol.proj.transform(e.coordinate, "EPSG:3857" , "EPSG:4326" );
const authentication = new arcgisRest.ApiKey({
key : apiKey
});
arcgisRest
.reverseGeocode(coords, {
authentication
})
.then( ( result ) => {
const message =
` ${result.address.LongLabel} <br>` + ` ${result.location.x.toLocaleString()} , ${result.location.y.toLocaleString()} ` ;
popup.show(e.coordinate, message);
})
.catch( ( error ) => {
popup.hide();
console .error(error);
});
});
const basemapId = "ArcGIS:Navigation" ;
olms.apply(map, "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey);
</ script >
</ body >
</ html >
Show more lines
Run the appIn CodePen , run your code to display the map.
Click on the map to reverse geocode the clicked point and display a pop-up with the closest address and coordinates.
What's next?Learn how to use additional ArcGIS location services in these tutorials: