Skip To Content ArcGIS for Developers Sign In Dashboard

Reverse geocode

Estimated address for a geographic point


// Copyright 2019 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:
// 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.

using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.Tasks.Geocoding;
using Esri.ArcGISRuntime.UI;
using Esri.ArcGISRuntime.UI.Controls;
using Foundation;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using UIKit;

namespace ArcGISRuntimeXamarin.Samples.ReverseGeocode
        "Reverse geocode",
        "Use an online geocoding service to find the address for a point.",
    public class ReverseGeocode : UIViewController
        // Hold references to UI controls.
        private MapView _myMapView;

        // Service Uri to be provided to the LocatorTask (geocoder).
        private readonly Uri _serviceUri = new Uri("");

        // The LocatorTask provides geocoding services.
        private LocatorTask _geocoder;

        public ReverseGeocode()
            Title = "Reverse geocode";

        private async void Initialize()
            // Create new Map with basemap.
            Map myMap = new Map(Basemap.CreateImageryWithLabels());

            // Provide used Map to the MapView.
            _myMapView.Map = myMap;

            // Add a graphics overlay to the map for showing where the user tapped.
            _myMapView.GraphicsOverlays.Add(new GraphicsOverlay());

            // Enable tap-for-info pattern on results.
            _myMapView.GeoViewTapped += _myMapView_GeoViewTapped;

            // Initialize the LocatorTask with the provided service Uri.
                _geocoder = await LocatorTask.CreateAsync(_serviceUri);
            catch (Exception e)
                new UIAlertView("Error", e.ToString(), (IUIAlertViewDelegate) null, "OK", null).Show();

            // Set the initial viewpoint.
            await _myMapView.SetViewpointCenterAsync(34.058, -117.195, 5e4);

        private async void _myMapView_GeoViewTapped(object sender, GeoViewInputEventArgs e)
                // Clear the existing graphics & callouts.

                // Add a graphic for the tapped point.
                Graphic pinGraphic = await GraphicForPoint(e.Location);

                // Normalize the geometry - needed if the user crosses the international date line.
                MapPoint normalizedPoint = (MapPoint)GeometryEngine.NormalizeCentralMeridian(e.Location);

                // Reverse geocode to get addresses.
                IReadOnlyList<GeocodeResult> addresses = await _geocoder.ReverseGeocodeAsync(normalizedPoint);

                // Get the first result.
                GeocodeResult address = addresses.First();

                // Use the city and region for the Callout Title.
                string calloutTitle = address.Attributes["Address"].ToString();

                // Use the metro area for the Callout Detail.
                string calloutDetail = address.Attributes["City"] +
                                       " " + address.Attributes["Region"] +
                                       " " + address.Attributes["CountryCode"];

                // Define the callout.
                CalloutDefinition calloutBody = new CalloutDefinition(calloutTitle, calloutDetail);

                // Show the callout on the map at the tapped location.
                _myMapView.ShowCalloutForGeoElement(pinGraphic, e.Position, calloutBody);
            catch (Exception ex)
                new UIAlertView("No results", "No results found.", (IUIAlertViewDelegate) null, "OK", null).Show();

        private async Task<Graphic> GraphicForPoint(MapPoint point)
            // Get current assembly that contains the image.
            Assembly currentAssembly = Assembly.GetExecutingAssembly();

            // Get image as a stream from the resources.
            // Picture is defined as EmbeddedResource and DoNotCopy.
            Stream resourceStream = currentAssembly.GetManifestResourceStream(

            // Create new symbol using asynchronous factory method from stream.
            PictureMarkerSymbol pinSymbol = await PictureMarkerSymbol.CreateAsync(resourceStream);
            pinSymbol.Width = 60;
            pinSymbol.Height = 60;
            // The image is a pin; offset the image so that the pinpoint
            //     is on the point rather than the image's true center.
            pinSymbol.LeaderOffsetX = 30;
            pinSymbol.OffsetY = 14;
            return new Graphic(point, pinSymbol);

        public override void LoadView()
            // Create the views.
            View = new UIView();

            _myMapView = new MapView();
            _myMapView.TranslatesAutoresizingMaskIntoConstraints = false;

            UILabel helpLabel = new UILabel
                Text = "Tap to find the nearest address.",
                AdjustsFontSizeToFitWidth = true,
                TextAlignment = UITextAlignment.Center,
                BackgroundColor = UIColor.FromWhiteAlpha(0, .6f),
                TextColor = UIColor.White,
                Lines = 1,
                TranslatesAutoresizingMaskIntoConstraints = false

            // Add the views.
            View.AddSubviews(_myMapView, helpLabel);

            // Lay out the views.


        public override void ViewDidLoad()

In this topic
  1. Code