Reverse geocode

View inC++QMLView on GitHubSample viewer app

Use an online service to find the address for a tapped point.

screenshot

Use case

You might use a geocoder to find a customer's delivery address based on the location returned by their device's GPS.

How to use the sample

Tap the map to see the nearest address displayed in a callout.

How it works

  1. Create the Map with a Basemap.
  2. Create a LocatorTask using a URL.
  3. Set the ReverseGeocodeParameters for the LocatorTask and specify the geocode's attributes.
  4. Get the matching results from the GeocodeResult using ReverseGeocodeWithParameters
  5. Change the attributes of the MapView's CalloutData and display the location using a Callout

Relevant API

  • GeocodeParameters
  • LocatorTask
  • ReverseGeocodeParameters

Offline data

Read more about how to set up the sample's offline data here.

Link Local Location
pin PNG file <userhome>/ArcGIS/Runtime/Data/symbol/pin.png

About the data

This sample uses the World Geocoding Service.

Tags

address, geocode, locate, reverse geocode, search

Sample Code

ReverseGeocodeOnline.qml
Use dark colors for code blocksCopy
1
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// [WriteFile Name=ReverseGeocodeOnline, Category=Search]
// [Legal]
// Copyright 2020 Esri.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]

import QtQuick
import QtQuick.Controls
import Esri.ArcGISExtras
import Esri.ArcGISRuntime
import Esri.ArcGISRuntime.Toolkit

Rectangle {
    id: rootRectangle
    clip: true
    width: 800
    height: 600

    property Point clickedPoint: null

    MapView {
        id: mapView
        anchors.fill: parent

        Component.onCompleted: {
            // Set the focus on MapView to initially enable keyboard navigation
            forceActiveFocus();
        }

        Callout {
            id: callout
            calloutData: parent.calloutData
            accessoryButtonVisible: false
            leaderPosition: Callout.LeaderPosition.Top
            padding: 5
        }
        onMouseClicked: mouse => {
            clickedPoint = mapView.screenToLocation(mouse.x, mouse.y);
            mapView.identifyGraphicsOverlayWithMaxResults(graphicsOverlay, mouse.x, mouse.y, 5, false, 1);
        }

        onIdentifyGraphicsOverlayStatusChanged: {
            if (identifyGraphicsOverlayStatus === Enums.TaskStatusCompleted) {
                if (locatorTask.geocodeStatus !== Enums.TaskStatusInProgress) {
                    locatorTask.reverseGeocodeWithParameters(clickedPoint, reverseGeocodeParameters);
                }
            }
        }

        Map {
            Basemap {
                initStyle: Enums.BasemapStyleArcGISImagery
            }

            ViewpointCenter {
                Point {
                    x: -13042254.715252
                    y: 3857970.236806
                    SpatialReference {
                        wkid: 3857
                    }
                }
                targetScale: 30000
            }
        }

        GraphicsOverlay {
            id: graphicsOverlay

            Graphic {
                id: pointGraphic
                PictureMarkerSymbol {
                    url: "qrc:/Samples/Search/ReverseGeocodeOnline/pin_circle_red.png"
                    height: 40
                    width: 40
                    offsetY: height/2
                }
            }
        }

        ReverseGeocodeParameters {
            id: reverseGeocodeParameters
            outputSpatialReference: mapView.spatialReference
        }

        LocatorTask {
            id: locatorTask

            // An ArcGIS Developer API key is required to utilize the world geocoding service
            url: "https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"

            onGeocodeStatusChanged: {
                if (geocodeStatus === Enums.TaskStatusCompleted) {
                    if (geocodeResults.length > 0) {
                        const address = geocodeResults[0].label;
                        const splitIndex = address.indexOf(",");
                        mapView.setViewpointCenter(geocodeResults[0].displayLocation);
                        mapView.calloutData.location = clickedPoint;
                        mapView.calloutData.title = address.substring(0, splitIndex < 0 ? undefined: splitIndex).trim();
                        mapView.calloutData.detail = address.substring(splitIndex + 1).trim();
                        callout.showCallout();
                        pointGraphic.geometry = clickedPoint;
                    }
                }
            }
        }
    }

    BusyIndicator {
        anchors.centerIn: parent
        visible: true
        running: locatorTask.geocodeStatus === Enums.TaskStatusInProgress
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.