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, 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. To simplify accessing the Geocoding service, you use the locator
module.
In this tutorial, you will use the locator
to reverse geocode and find the closest address to your clicked location on the map.
To learn more about geocoding and searching for places, visit Geocode and search in the Mapping and location services guide.
Prerequisites ArcGIS Accounts:You need an ArcGIS Location Platform or ArcGIS Online account.
Steps Create a new penYou need an access token with the correct privileges to access the location services used in this tutorial.
esriConfig.apiKey
to your access token.
Use dark colors for code blocks
1
2
3
4
5
6
7
8
9
var esriConfig = {
apiKey: "YOUR_ACCESS_TOKEN",
};
To learn about other ways to get an access token, go to Types of authentication.
Update the mapA streets basemap layer is typically used in geocoding applications. Update the basemap
attribute to use the arcgis/navigation
basemap layer and change the position of the map to center on Quito.
basemap
attribute from arcgis/topographic
to arcgis/navigation
. Then, update the center
attribute to -78.50169,-0.21489
, and set the zoom
attribute to 12
.
Expand
Use dark colors for code blocks1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<arcgis-map basemap="arcgis/navigation" center="-78.50169, -0.21489" zoom="12">
<arcgis-zoom position="top-left"></arcgis-zoom>
</arcgis-map>
Add a <script>
tag in the <body>
following the <arcgis-map>
component. Use $arcgis.import()
to add the locator
module.
The ArcGIS Maps SDK for JavaScript is available via CDN and npm, but this tutorial is based on CDN. The $arcgis.import
global function accepts a module path or array of module paths, and returns a promise that resolves with the requested modules. This function can only be used when working with the CDN; otherwise, use the standard import syntax. To learn more about the SDK's different modules, visit the References page.
Use the document.querySelector() method to access the map component.
Expand
Use dark colors for code blocks1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<script type="module">
const locator = await $arcgis.import("@arcgis/core/rest/locator.js");
const viewElement = document.querySelector("arcgis-map");
</script>
Create an event listener to listen for the map component's arcgisViewReadyChange
event.
Expand
Use dark colors for code blocks1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<script type="module">
const locator = await $arcgis.import("@arcgis/core/rest/locator.js");
const viewElement = document.querySelector("arcgis-map");
viewElement.addEventListener("arcgisViewReadyChange", () => {
});
</script>
Use the locator
module to access the Geocoding service and the reverseGeocode
operation.
serviceUrl
, to reference the Geocoding service.
Expand
Use dark colors for code blocks1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
viewElement.addEventListener("arcgisViewReadyChange", () => {
const serviceUrl = "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
});
Use an event listener to capture a click location on the map and then call the Geocoding service. The service returns an address if an address is found, or an error if it cannot find a result. Display the results in a pop-up with the latitude, longitude, and address.
In the arcgisViewReadyChange
event listener, add an arcgisViewClick
event handler to the map component. Create params
and set the location
to evt.detail.mapPoint
.
Expand
Use dark colors for code blocks1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const serviceUrl = "http://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer";
viewElement.addEventListener("arcgisViewClick", (evt) => {
const params = {
location: evt.detail.mapPoint,
};
});
Create a showAddress
function to display coordinates and the corresponding address within a pop-up.
Expand
Use dark colors for code blocks1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
viewElement.addEventListener("arcgisViewClick", (evt) => {
const params = {
location: evt.detail.mapPoint,
};
});
function showAddress(address, pt) {
viewElement.openPopup({
title:
+Math.round(pt.longitude * 100000) / 100000 +
", " +
Math.round(pt.latitude * 100000) / 100000,
content: address,
location: pt,
});
}
Update the arcgisViewClick
handler to call locationToAddress
to reverse geocode the mapPoint
. Use the showAddress
function to display a pop-up with the results.
Expand
Use dark colors for code blocks1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
viewElement.addEventListener("arcgisViewClick", (evt) => {
const params = {
location: evt.detail.mapPoint,
};
locator.locationToAddress(serviceUrl, params).then(
(response) => {
// Show the address found
const address = response.address;
showAddress(address, evt.detail.mapPoint);
},
(error) => {
// Show no address found
showAddress("No address found.", evt.detail.mapPoint);
},
);
});
In CodePen, run your code to display the map.
Click on the map to reverse geocode a location and return a pop-up with the closest address.
What's next?Learn how to use additional API features and ArcGIS services in these tutorials:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4