Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for Qt

Reverse geocode

Sample Viewer View Sample on GitHub

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

  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

Tags

address, geocode, locate, reverse geocode, search

Sample Code

import QtQuick 2.6
import QtQuick.Controls 2.6
import Esri.ArcGISExtras 1.1
import Esri.ArcGISRuntime 100.9
import Esri.ArcGISRuntime.Toolkit.Controls 100.9 // needed to use Callout in QML

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

    property Point clickedPoint: null

    MapView {
        id: mapView
        anchors.fill: parent

        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 {
            BasemapImageryWithLabels {}

            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
            url: "https://geocode.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
    }
}