Find address

View inC++QMLView on GitHubSample viewer app

Find the location for an address.

screenshot

Use case

A user can input a raw address into your app's search bar and zoom to the address location.

How to use the sample

For simplicity, the sample comes loaded with a set of suggested addresses. Choose an address from the suggestions or submit your own address to show its location on the map in a callout.

How it works

  1. Create a LocatorTask using the URL to a locator service.
  2. Set the GeocodeParameters for the locator task and specify the geocode's attributes.
  3. Get the matching results from the geocodeResults using locatorTask.geocodeWithParameters(addressString, geocodeParameters).
  4. Create a Graphic with the geocode result's location and store the geocode result's attributes in the graphic's attributes.
  5. Show the graphic in a GraphicsOverlay.

Relevant API

  • GeocodeParameters
  • GeocodeResult
  • LocatorTask

About the data

This sample uses the World Geocoding Service.

Tags

address, geocode, locator, search

Sample Code

FindAddress.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// [WriteFile Name=FindAddress, Category=Search]
// [Legal]
// Copyright 2016 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 2.6
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import Esri.ArcGISRuntime 100.15
import Esri.ArcGISRuntime.Toolkit 100.15

Rectangle {
    id: root
    width: 800
    height: 600

    property string calloutText
    property Point calloutLocation

    // Create MapView that contains a Map with the Imagery with Labels Basemap
    MapView {
        id: mapView
        anchors.fill: parent

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

        // Create a GraphicsOverlay to display the geocode results
        GraphicsOverlay {
            id: graphicsOverlay

            SimpleRenderer {
                PictureMarkerSymbol {
                    url: "qrc:/Samples/Search/FindAddress/pin_circle_red.png"
                    width: 35
                    height: 35
                    offsetY: height / 2 // the tip of the pin will point to the location
                }
            }
        }

        Map {
            Basemap {
                initStyle: Enums.BasemapStyleArcGISImagery
            }
        }

        // show the callout once the identify is complete
        onIdentifyGraphicsOverlayStatusChanged: {
            if (identifyGraphicsOverlayStatus === Enums.TaskStatusCompleted) {
                if (identifyGraphicsOverlayResult.graphics.length > 0) {
                    // setup the attributes
                    calloutText = identifyGraphicsOverlayResult.graphics[0].attributes.attributeValue("Match_addr");

                    // show the callout
                    callout.showCallout();
                }
            }
        }

        // perform identify operation on mapview
        onMouseClicked: {
            if (callout.visible)
                callout.dismiss();
            calloutLocation = mouse.mapPoint;
            mapView.identifyGraphicsOverlay(graphicsOverlay, mouse.x, mouse.y, 2, false);
        }

        // hide callout after navigation
        onViewpointChanged: {
            if (callout.visible)
                callout.dismiss();
        }

        calloutData {
            location:  calloutLocation
            title: calloutText
        }

        // map callout window
        Callout {
            id: callout
            borderWidth: 1
            calloutData: parent.calloutData
            borderColor: "lightgrey"
            accessoryButtonHidden: true
            maxWidth: root.width * 0.75
            leaderPosition: leaderPositionEnum.Automatic
        }
    }

    //! [FindAddress create LocatorTask]
    // Create a locator task using the World Geocoding Service
    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"

        // handle the result once the geocode status is complete
        onGeocodeStatusChanged: {
            if (geocodeStatus === Enums.TaskStatusCompleted) {
                if (geocodeResults.length > 0) {
                    graphicsOverlay.graphics.clear();
                    const graphic = ArcGISRuntimeEnvironment.createObject("Graphic");
                    graphic.geometry = geocodeResults[0].displayLocation;
                    graphic.attributes.attributesJson = geocodeResults[0].attributes;
                    graphicsOverlay.graphics.append(graphic);
                    mapView.setViewpointCenterAndScale(geocodeResults[0].extent.center, 8000);
                }
            }
        }
    }

    // Create geocode parameters
    GeocodeParameters {
        id: geocodeParameters
        minScore: 75
        resultAttributeNames: ["Place_addr", "Match_addr"]
    }
    //! [FindAddress create LocatorTask]

    // search bar for geocoding
    Column {
        anchors {
            fill: parent
            margins: 10
        }

        Rectangle {
            color: "#f7f8fa"
            border {
                color: "#7B7C7D"
            }
            radius: 2
            width: parent.width
            height: childrenRect.height

            GridLayout {
                width: parent.width
                columns: 3
                TextField {
                    Layout.margins: 5
                    Layout.fillWidth: true
                    id: textField
                    font.pixelSize: 14
                    placeholderText: "Type in an address"
                    selectByMouse: true

                    Keys.onEnterPressed: geocodeAddress();
                    Keys.onReturnPressed: geocodeAddress();

                    function geocodeAddress() {
                        //! [FindAddress geocodeWithParameters]
                        locatorTask.geocodeWithParameters(textField.text, geocodeParameters);
                        //! [FindAddress geocodeWithParameters]
                        suggestView.visible = false;
                        Qt.inputMethod.hide();
                    }
                }

                Rectangle {
                    Layout.margins: 5
                    width: height
                    height: textField.height
                    color: "#f7f8fa"
                    visible: textField.length === 0
                    enabled: visible
                    Image {
                        anchors.fill: parent
                        source: "qrc:/Samples/Search/FindAddress/ic_menu_collapsedencircled_light_d.png"
                        MouseArea {
                            anchors.fill: parent
                            onClicked: {
                                textField.focus = true;
                                suggestView.visible = !suggestView.visible;
                            }
                        }
                    }
                }

                Rectangle {
                    Layout.margins: 5
                    width: height
                    color: "transparent"
                    height: textField.height
                    visible: textField.length !== 0
                    enabled: visible
                    Image {
                        anchors.fill: parent
                        source: "qrc:/Samples/Search/FindAddress/ic_menu_closeclear_light_d.png"

                        MouseArea {
                            anchors.fill: parent
                            onClicked: {
                                textField.text = "";
                                if (callout.visible)
                                    callout.dismiss();
                                graphicsOverlay.graphics.clear();
                            }
                        }
                    }
                }
            }
        }

        // show a drop down of suggested locations
        ListView {
            id: suggestView
            height: 300
            width: textField.width
            visible: false
            clip: true
            model: geocodeSuggestions
            delegate: Component {
                Rectangle {
                    id: rect
                    width: parent.width
                    height: 25
                    color: "#f7f8fa"

                    Rectangle {
                        anchors {
                            top: parent.top;
                            left: parent.left;
                            right: parent.right;
                            topMargin: -5
                            leftMargin: 20
                            rightMargin: 20
                        }
                        color: "darkgrey"
                        height: 1
                    }

                    Text {
                        text: name
                        anchors {
                            fill: parent
                            leftMargin: 5
                        }

                        font.pixelSize: 14
                    }

                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            textField.text = name;
                            suggestView.visible = false;
                            locatorTask.geocodeWithParameters(name, geocodeParameters);
                            Qt.inputMethod.hide();
                        }
                    }
                }
            }
        }
    }

    ListModel {
        id: geocodeSuggestions
        ListElement { name: "277 N Avenida Caballeros, Palm Springs, CA" }
        ListElement { name: "380 New York St, Redlands, CA 92373" }
        ListElement { name: "Београд" }
        ListElement { name: "Москва" }
        ListElement { name: "北京" }
    }
}

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