Geocoding is the process of converting text to a complete address with a location.
You can use thearcgis-rest-geocoding package to access the ArcGIS Geocoding service.
Steps
-
Install and reference the
arcgis-rest-geocodingandarcgis-rest-requestpackages. - Set an access token to authenticate the request.
- Define parameters to pass to the service.
- Call the service and handle the results.
Address geocoding
Address geocoding, also known as forward geocoding, allows you to search for addresses or places. In ArcGIS, this is referred to as finding address candidates.
Search for an address
Learn how to find address candidates with the geocode method.
import { ApiKeyManager } from "@esri/arcgis-rest-request";
import { geocode } from "@esri/arcgis-rest-geocoding";
const accessToken = "YOUR_ACCESS_TOKEN";
const authentication = ApiKeyManager.fromKey(accessToken);
geocode({
address: "1600 Pennsylvania Ave",
postal: 20500,
countryCode: "USA",
authentication
}).then((response) => {
console.log(JSON.stringify(response, null, 2));
});Autosuggest
Autosuggest can help find address candidates with partial text input when building interactive geocoding controls.
Search with autosuggest
Learn how to use autosuggest candidates to find addresses with the suggest method.
import { ApiKeyManager } from "@esri/arcgis-rest-request";
import { suggest } from "@esri/arcgis-rest-geocoding";
const accessToken = "YOUR_ACCESS_TOKEN";
const authentication = ApiKeyManager.fromKey(accessToken);
suggest("200 R", {
authentication,
params: {
location: {
x: -123.102,
y: 49.234,
spatialReference: { wkid: 4326 }
},
maxSuggestions: 5
}
}).then((response) => {
console.log(JSON.stringify(response, null, 2));
});Place geocoding
Place geocoding is the process of searching for addresses for businesses, administrative locations, and geographic features. You can search for the name of a place and/or provide categories to filter the results.
Find place addresses
Search for coffee shops near a given location using the geocode method.
import { ApiKeyManager } from "@esri/arcgis-rest-request";
import { geocode } from "@esri/arcgis-rest-geocoding";
const accessToken = "YOUR_ACCESS_TOKEN";
const authentication = ApiKeyManager.fromKey(accessToken);
geocode({
params: {
category: "Coffee shop",
location: "144.9850916862488,-37.798297645411",
maxLocations: 20
},
outFields: "*",
authentication
}).then((response) => {
console.log(JSON.stringify(response, null, 2));
});Reverse geocoding
Reverse geocoding allows you to convert a location into a complete address. You can also use it to find the nearest address to your current location or show the closest address when you tap the map.
Reverse geocode
Learn how to reverse geocode with the reverse method.
import { ApiKeyManager } from "@esri/arcgis-rest-request";
import { reverseGeocode } from "@esri/arcgis-rest-geocoding";
const accessToken = "YOUR_ACCESS_TOKEN";
const authentication = ApiKeyManager.fromKey(accessToken);
reverseGeocode({
location: [-79.3871, 43.6426], // [lon, lat]
authentication
}).then((response) => {
console.log(JSON.stringify(response, null, 2));
});