Use an online service to find the address for a tapped point.
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
- Create the
Map
with aBasemap
. - Create a
LocatorTask
using a URL. - Set the
ReverseGeocodeParameters
for theLocatorTask
and specify the geocode's attributes. - Get the matching results from the
GeocodeResult
usingReverseGeocodeWithParameters
- Change the attributes of the
MapView
'sCalloutData
and display the location using aCallout
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
// [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 2.6
import QtQuick.Controls 2.6
import Esri.ArcGISExtras 1.1
import Esri.ArcGISRuntime 100.15
import Esri.ArcGISRuntime.Toolkit 100.15
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
accessoryButtonHidden: true
leaderPosition: leaderPositionEnum.Top
padding: 5
}
onMouseClicked: {
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
}
}