Learn how to find an address or place with a search bar and the geocoding service A geocoding service is a service that can search for addresses, place addresses, businesses, reverse geocode coordinates to addresses, provide suggestions for places, and perform bulk geocoding. It is hosted by Esri as the ArcGIS Geocoding service and can also be hosted in ArcGIS Enterprise. Learn more .

search for an address

Geocoding is the process of converting address or place A place, also known as a point of interest (POI), is a location that represents a business, administrative entity, or geographic feature around the world. A place can also have attributes associated with it, such as a name, address, category, and ID. Learn more text into a location A location is a position or region (point, line, or polygon) on the earth's surface. Learn more . The geocoding service A geocoding service is a service that can search for addresses, place addresses, businesses, reverse geocode coordinates to addresses, provide suggestions for places, and perform bulk geocoding. It is hosted by Esri as the ArcGIS Geocoding service and can also be hosted in ArcGIS Enterprise. Learn more can search for an address or a place and perform reverse geocoding Reverse geocoding is the process of converting a point to its nearest address or place. Learn more .

In this tutorial, you use a search bar in the user interface to access the Geocoding service and search for addresses and places.

Prerequisites

Before starting this tutorial:

Optionally, you may want to install the ArcGIS Maps SDK for .NET to get access to project templates in Visual Studio (Windows only) and offline copies of the NuGet packages.

Set up authentication

To access the secure ArcGIS location services ArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. Learn more used in this tutorial, you must implement API key authentication API key authentication is a type of authentication that uses an API key to authenticate requests to ArcGIS services and secure portal items. Learn more or user authentication User authentication is a type of authentication that allows users with an ArcGIS account to sign into an application and allow it to access ArcGIS content, services, and resources on their behalf. The typical authorization protocol used is OAuth2.0. Learn more using an ArcGIS Location Platform An ArcGIS Location Platform account, formerly known as an ArcGIS Developer account, is an identity associated with an ArcGIS Location Platform subscription. Learn more or an ArcGIS Online An ArcGIS Online account, also known as an ArcGIS Organization account, is an identity associated with an ArcGIS Online subscription. It can be used to access ArcGIS tools and develop applications with ArcGIS location services for an organization. Learn more account.

To complete this tutorial, click on the tab in the switcher below for your authentication type of choice, either API key authentication or User authentication.

Create a new API key access token An access token is an authorization string that provides access to secure ArcGIS content, data, and services. Its capabilities are determined by the privileges it supports. It is obtained by implementing API key authentication, User authentication, or App authentication. Learn more with privileges Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. Learn more to access the secure resources used in this tutorial.

  1. Complete the Create an API key tutorial and create an API key with the following privilege(s) Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. Learn more :

    • Privileges
      • Location services > Basemaps
      • Location services > Geocoding
  2. Copy and paste the API key access token into a safe location. It will be used in a later step.

Develop or download

You have two options for completing this tutorial:

  1. Option 1: Develop the code or
  2. Option 2: Download the completed solution

Option 1: Develop the code

To start the tutorial, complete the Display a map tutorial. This creates a map to display the Santa Monica Mountains in California using the topographic basemap from the ArcGIS Basemap Styles service The ArcGIS Basemap Styles service, also referred to as the Basemap Styles service, is a location service that provides basemap styles and data for the world. It returns styles as Mapbox styles and web maps, and data as vector tiles and/or map tiles. It supports all of the styles in the ArcGIS Basemap style and Open Basemap style family. An ArcGIS Location Platform or ArcGIS Online account is required to use the service. Learn more .

Open a Visual Studio solution

  1. Open the Visual Studio solution you created by completing the Display a map tutorial.
  2. Continue following the instructions to:
    • Configure authentication in your app so that your app users can access the ArcGIS Geocoding service A geocoding service is a service that can search for addresses, place addresses, businesses, reverse geocode coordinates to addresses, provide suggestions for places, and perform bulk geocoding. It is hosted by Esri as the ArcGIS Geocoding service and can also be hosted in ArcGIS Enterprise. Learn more to find addresses and show their location on the map.
    • Add functionality to search for an address or place using the ArcGIS Geocoding service A geocoding service is a service that can search for addresses, place addresses, businesses, reverse geocode coordinates to addresses, provide suggestions for places, and perform bulk geocoding. It is hosted by Esri as the ArcGIS Geocoding service and can also be hosted in ArcGIS Enterprise. Learn more .

Update the tutorial name used in the project (optional)

The Visual Studio solution, project, and the namespace for all classes currently use the name DisplayAMap. Follow the steps below if you prefer the name to reflect the current tutorial. These steps are not required, your code will still work if you keep the original name.

Set developer credentials

If you implemented API key authentication API key authentication is a type of authentication that uses an API key to authenticate requests to ArcGIS services and secure portal items. Learn more in the Display a map tutorial, the API key access token will only have the Basemaps privilege. The Search for an address tutorial requires the Geocoding privilege to search for an address using the LocatorTask. To create an API Key access token that has the Basemaps and Geocoding privileges, see the Set up authentication step and then follow the instructions below.

  1. In the Solution Explorer, expand the node for App.xaml, and double-click App.xaml.cs to open it.

  2. In the App class, in the override for the OnStartup() function, set the ApiKey property on ArcGISRuntimeEnvironment with your new API key (with additional privileges besides basemap).

    App.xaml.cs
    public partial class App : Application
    {
    protected override void OnStartup(StartupEventArgs e)
    {
    base.OnStartup(e);
    // Set the access token for ArcGIS Maps SDK for .NET.
    Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey = "YOUR_ACCESS_TOKEN";
    }
    }
    }
  3. Save and close the App.xaml.cs file.

Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

Add a graphics overlay

A graphics overlay A graphics overlay is a client-side, temporary container of graphics to display on a map view or scene view. Learn more is a container for graphics A graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. Learn more . Graphics overlays are typically used to display temporary information, such as the results of an address search operation, and are displayed on top of all other layers A layer is a reference to a collection of geographic data that is used to access and display data. The data for layers are typically provided by the basemap layer service and data services. Learn more . They do not persist when you save the map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more .

  1. In the Visual Studio > Solution Explorer, double-click MapViewModel.cs to open the file.

  2. Add additional required using statements to your MapViewModel class. Geocoding contains classes used for geocoding (finding geographic locations from an address). The Esri.ArcGISRuntime.UI namespace contains the GraphicsOverlay and Graphic classes and Esri.ArcGISRuntime.Symbology contains the classes that define the symbols for displaying them.

    using System;
    using System.Collections.Generic;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Geocoding;
    using Esri.ArcGISRuntime.UI;
    using System.Drawing;
    using System.Linq;
    using System.Threading.Tasks;
  3. In the view model, create a new property named GraphicsOverlays. This is a collection of GraphicsOverlay that will store geocode result graphics (address location and label).

    45 collapsed lines
    // Copyright 2021 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
    //
    // https://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.
    using System;
    using System.Collections.Generic;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Geocoding;
    using Esri.ArcGISRuntime.UI;
    using System.Drawing;
    using System.Linq;
    using System.Threading.Tasks;
    namespace SearchForAnAddress
    {
    class MapViewModel : INotifyPropertyChanged
    {
    public MapViewModel()
    {
    SetupMap();
    }
    public event PropertyChangedEventHandler? PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
    var propertyChangedHandler = PropertyChanged;
    if (propertyChangedHandler != null)
    propertyChangedHandler(this, new PropertyChangedEventArgs(propertyName));
    }
    private Map? _map;
    public Map? Map
    {
    get { return _map; }
    set
    {
    _map = value;
    OnPropertyChanged();
    }
    }
    private GraphicsOverlayCollection? _graphicsOverlayCollection;
    public GraphicsOverlayCollection? GraphicsOverlays
    {
    get { return _graphicsOverlayCollection; }
    set
    {
    _graphicsOverlayCollection = value;
    OnPropertyChanged();
    }
    }
    17 collapsed lines
    private void SetupMap()
    {
    // Create a new map with a 'topographic vector' basemap.
    Map map = new Map(BasemapStyle.ArcGISTopographic);
    // Set the initial viewpoint around the Santa Monica Mountains in California.
    var mapCenterPoint = new MapPoint(-118.805, 34.027, SpatialReferences.Wgs84);
    map.InitialViewpoint = new Viewpoint(mapCenterPoint, 100000);
    // Set the view model "Map" property.
    this.Map = map;
    }
    }
    }
  4. At the end of the SetupMap function, add code to create a new GraphicsOverlay, add it to a collection, and assign it to the GraphicsOverlays property.

    76 collapsed lines
    // Copyright 2021 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
    //
    // https://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.
    using System;
    using System.Collections.Generic;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Geocoding;
    using Esri.ArcGISRuntime.UI;
    using System.Drawing;
    using System.Linq;
    using System.Threading.Tasks;
    namespace SearchForAnAddress
    {
    class MapViewModel : INotifyPropertyChanged
    {
    public MapViewModel()
    {
    SetupMap();
    }
    public event PropertyChangedEventHandler? PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
    var propertyChangedHandler = PropertyChanged;
    if (propertyChangedHandler != null)
    propertyChangedHandler(this, new PropertyChangedEventArgs(propertyName));
    }
    private Map? _map;
    public Map? Map
    {
    get { return _map; }
    set
    {
    _map = value;
    OnPropertyChanged();
    }
    }
    private GraphicsOverlayCollection? _graphicsOverlayCollection;
    public GraphicsOverlayCollection? GraphicsOverlays
    {
    get { return _graphicsOverlayCollection; }
    set
    {
    _graphicsOverlayCollection = value;
    OnPropertyChanged();
    }
    }
    private void SetupMap()
    {
    // Create a new map with a 'topographic vector' basemap.
    Map map = new Map(BasemapStyle.ArcGISTopographic);
    // Set the initial viewpoint around the Santa Monica Mountains in California.
    var mapCenterPoint = new MapPoint(-118.805, 34.027, SpatialReferences.Wgs84);
    map.InitialViewpoint = new Viewpoint(mapCenterPoint, 100000);
    // Set the view model "Map" property.
    this.Map = map;
    // Set the view model "GraphicsOverlays" property.
    GraphicsOverlay overlay = new GraphicsOverlay();
    GraphicsOverlayCollection overlayCollection = new GraphicsOverlayCollection
    {
    overlay
    };
    this.GraphicsOverlays = overlayCollection;
    }
    3 collapsed lines
    }
    }
  5. In the Visual Studio > Solution Explorer, double-click MainWindow.xaml to open the file.

  6. Use data binding to bind the GraphicsOverlays property of the MapViewModel to the MapView control.

    15 collapsed lines
    <Window x:Class="SearchForAnAddress.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:SearchForAnAddress"
    xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
    <local:MapViewModel x:Key="MapViewModel" />
    </Window.Resources>
    <Grid>
    <esri:MapView x:Name="MainMapView"
    Map="{Binding Map, Source={StaticResource MapViewModel}}"
    GraphicsOverlays="{Binding GraphicsOverlays, Source={StaticResource MapViewModel}}"/>
    4 collapsed lines
    </Grid>
    </Window>

Add a UI for user input

The user will type an address into a text box and then click a button to execute a search. The MapViewModel class contains the logic to execute the search, but the controls are managed by the view.

  1. In the Visual Studio > Solution Explorer, double-click MainWindow.xaml to open the file.

  2. Add a Border above the map view that contains the address search controls.

    13 collapsed lines
    <Window x:Class="SearchForAnAddress.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:SearchForAnAddress"
    xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
    <local:MapViewModel x:Key="MapViewModel" />
    </Window.Resources>
    <Grid>
    <esri:MapView x:Name="MainMapView"
    Map="{Binding Map, Source={StaticResource MapViewModel}}"
    GraphicsOverlays="{Binding GraphicsOverlays, Source={StaticResource MapViewModel}}"/>
    <Border Background="LightBlue"
    HorizontalAlignment="Center" VerticalAlignment="Top"
    Width="300" Height="60"
    Margin="0,10">
    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition Height="20"/>
    <RowDefinition Height="30" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="3*" />
    <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <TextBlock Text="Search for an address"
    Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"
    TextAlignment="Center" VerticalAlignment="Center"
    FontWeight="SemiBold" />
    <TextBox x:Name="AddressTextBox"
    Grid.Row="1" Grid.Column="0"
    Margin="5"
    Text="380 New York Street, Redlands CA"/>
    <Button x:Name="SearchAddressButton"
    Grid.Row="1" Grid.Column="1"
    Margin="5"
    Content="Search"
    Click="SearchAddressButton_Click" />
    </Grid>
    </Border>
    </Grid>
    2 collapsed lines
    </Window>

Create a function to geocode an address

Geocoding is implemented with a locator A locator is an ArcGIS dataset that stores address information and the rules for translating descriptions of places (such as street addresses or place names) into spatial data that can be displayed on a map. Learn more , typically created by referencing a geocoding service A geocoding service is a service that can search for addresses, place addresses, businesses, reverse geocode coordinates to addresses, provide suggestions for places, and perform bulk geocoding. It is hosted by Esri as the ArcGIS Geocoding service and can also be hosted in ArcGIS Enterprise. Learn more such as the Geocoding service or, for offline geocoding, by referencing locator data contained in a mobile package A mobile package is a stand-alone Mobile Map Package (MMPK) or Mobile Scene Package (MSPK) file for use in offline applications built with ArcGIS Maps SDKs for Native Apps. Learn more . Geocoding parameters can be used to fine-tune the results, such as setting the maximum number of results or requesting additional attributes in the results.

  1. In the Visual Studio > Solution Explorer, double-click MapViewModel.cs to open the file.

  2. Add a new function to search for an address and return its geographic location. The address string and a spatial reference (for output locations) are passed to the function. The function is marked async since it will make asynchronous calls using the await keyword.

    76 collapsed lines
    // Copyright 2021 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
    //
    // https://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.
    using System;
    using System.Collections.Generic;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Geocoding;
    using Esri.ArcGISRuntime.UI;
    using System.Drawing;
    using System.Linq;
    using System.Threading.Tasks;
    namespace SearchForAnAddress
    {
    class MapViewModel : INotifyPropertyChanged
    {
    public MapViewModel()
    {
    SetupMap();
    }
    public event PropertyChangedEventHandler? PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
    var propertyChangedHandler = PropertyChanged;
    if (propertyChangedHandler != null)
    propertyChangedHandler(this, new PropertyChangedEventArgs(propertyName));
    }
    private Map? _map;
    public Map? Map
    {
    get { return _map; }
    set
    {
    _map = value;
    OnPropertyChanged();
    }
    }
    private GraphicsOverlayCollection? _graphicsOverlayCollection;
    public GraphicsOverlayCollection? GraphicsOverlays
    {
    get { return _graphicsOverlayCollection; }
    set
    {
    _graphicsOverlayCollection = value;
    OnPropertyChanged();
    }
    }
    private void SetupMap()
    {
    // Create a new map with a 'topographic vector' basemap.
    Map map = new Map(BasemapStyle.ArcGISTopographic);
    // Set the initial viewpoint around the Santa Monica Mountains in California.
    var mapCenterPoint = new MapPoint(-118.805, 34.027, SpatialReferences.Wgs84);
    map.InitialViewpoint = new Viewpoint(mapCenterPoint, 100000);
    // Set the view model "Map" property.
    this.Map = map;
    // Set the view model "GraphicsOverlays" property.
    GraphicsOverlay overlay = new GraphicsOverlay();
    GraphicsOverlayCollection overlayCollection = new GraphicsOverlayCollection
    {
    overlay
    };
    this.GraphicsOverlays = overlayCollection;
    }
    public async Task<MapPoint?> SearchAddress(string address, SpatialReference spatialReference)
    {
    MapPoint? addressLocation = null;
    try
    {
    }
    catch (Exception ex)
    {
    System.Windows.MessageBox.Show("Couldn't find address: " + ex.Message);
    }
    // Return the location of the geocode result.
    return addressLocation;
    }
    3 collapsed lines
    }
    }
  3. Get the GraphicsOverlay and clear all of its Graphics.

    89 collapsed lines
    // Copyright 2021 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
    //
    // https://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.
    using System;
    using System.Collections.Generic;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Geocoding;
    using Esri.ArcGISRuntime.UI;
    using System.Drawing;
    using System.Linq;
    using System.Threading.Tasks;
    namespace SearchForAnAddress
    {
    class MapViewModel : INotifyPropertyChanged
    {
    public MapViewModel()
    {
    SetupMap();
    }
    public event PropertyChangedEventHandler? PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
    var propertyChangedHandler = PropertyChanged;
    if (propertyChangedHandler != null)
    propertyChangedHandler(this, new PropertyChangedEventArgs(propertyName));
    }
    private Map? _map;
    public Map? Map
    {
    get { return _map; }
    set
    {
    _map = value;
    OnPropertyChanged();
    }
    }
    private GraphicsOverlayCollection? _graphicsOverlayCollection;
    public GraphicsOverlayCollection? GraphicsOverlays
    {
    get { return _graphicsOverlayCollection; }
    set
    {
    _graphicsOverlayCollection = value;
    OnPropertyChanged();
    }
    }
    private void SetupMap()
    {
    // Create a new map with a 'topographic vector' basemap.
    Map map = new Map(BasemapStyle.ArcGISTopographic);
    // Set the initial viewpoint around the Santa Monica Mountains in California.
    var mapCenterPoint = new MapPoint(-118.805, 34.027, SpatialReferences.Wgs84);
    map.InitialViewpoint = new Viewpoint(mapCenterPoint, 100000);
    // Set the view model "Map" property.
    this.Map = map;
    // Set the view model "GraphicsOverlays" property.
    GraphicsOverlay overlay = new GraphicsOverlay();
    GraphicsOverlayCollection overlayCollection = new GraphicsOverlayCollection
    {
    overlay
    };
    this.GraphicsOverlays = overlayCollection;
    }
    public async Task<MapPoint?> SearchAddress(string address, SpatialReference spatialReference)
    {
    MapPoint? addressLocation = null;
    try
    {
    // Get the first graphics overlay from the GraphicsOverlays and remove any previous result graphics.
    GraphicsOverlay? graphicsOverlay = this.GraphicsOverlays?.FirstOrDefault();
    graphicsOverlay?.Graphics.Clear();
    13 collapsed lines
    }
    catch (Exception ex)
    {
    System.Windows.MessageBox.Show("Couldn't find address: " + ex.Message);
    }
    // Return the location of the geocode result.
    return addressLocation;
    }
    }
    }
  4. Create a LocatorTask based on the Geocoding service.

    96 collapsed lines
    // Copyright 2021 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
    //
    // https://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.
    using System;
    using System.Collections.Generic;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Geocoding;
    using Esri.ArcGISRuntime.UI;
    using System.Drawing;
    using System.Linq;
    using System.Threading.Tasks;
    namespace SearchForAnAddress
    {
    class MapViewModel : INotifyPropertyChanged
    {
    public MapViewModel()
    {
    SetupMap();
    }
    public event PropertyChangedEventHandler? PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
    var propertyChangedHandler = PropertyChanged;
    if (propertyChangedHandler != null)
    propertyChangedHandler(this, new PropertyChangedEventArgs(propertyName));
    }
    private Map? _map;
    public Map? Map
    {
    get { return _map; }
    set
    {
    _map = value;
    OnPropertyChanged();
    }
    }
    private GraphicsOverlayCollection? _graphicsOverlayCollection;
    public GraphicsOverlayCollection? GraphicsOverlays
    {
    get { return _graphicsOverlayCollection; }
    set
    {
    _graphicsOverlayCollection = value;
    OnPropertyChanged();
    }
    }
    private void SetupMap()
    {
    // Create a new map with a 'topographic vector' basemap.
    Map map = new Map(BasemapStyle.ArcGISTopographic);
    // Set the initial viewpoint around the Santa Monica Mountains in California.
    var mapCenterPoint = new MapPoint(-118.805, 34.027, SpatialReferences.Wgs84);
    map.InitialViewpoint = new Viewpoint(mapCenterPoint, 100000);
    // Set the view model "Map" property.
    this.Map = map;
    // Set the view model "GraphicsOverlays" property.
    GraphicsOverlay overlay = new GraphicsOverlay();
    GraphicsOverlayCollection overlayCollection = new GraphicsOverlayCollection
    {
    overlay
    };
    this.GraphicsOverlays = overlayCollection;
    }
    public async Task<MapPoint?> SearchAddress(string address, SpatialReference spatialReference)
    {
    MapPoint? addressLocation = null;
    try
    {
    // Get the first graphics overlay from the GraphicsOverlays and remove any previous result graphics.
    GraphicsOverlay? graphicsOverlay = this.GraphicsOverlays?.FirstOrDefault();
    graphicsOverlay?.Graphics.Clear();
    // Create a locator task.
    LocatorTask locatorTask = new LocatorTask(new Uri("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"));
    13 collapsed lines
    }
    catch (Exception ex)
    {
    System.Windows.MessageBox.Show("Couldn't find address: " + ex.Message);
    }
    // Return the location of the geocode result.
    return addressLocation;
    }
    }
    }
  5. Create a new GeocodeParameters and define some of its properties.

    • Add the names of attributes to return to the GeocodeParameters.ResultAttributeNames collection. An asterisk (*) indicates all attributes.
    • Set the maximum number of results to be returned with GeocodeParameters.MaxResults. Results are ordered by score, so that the first result has the best match score (ranging from 0 for no match to 100 for the best match).
    • Set the spatial reference A spatial reference is a set of parameters, typically defined by a WKID, that define the coordinate system and spatial properties for geographic data. Applications use a spatial reference to correctly display the position of geographic data in a map or scene. Learn more for result locations with GeocodeParameters.OutputSpatialReference. By default, the output spatial reference is defined by the geocode service. For optimal performance when displaying the geocode result, you can ensure that returned coordinates match those of the map view by providing the map view’s spatial reference.
    100 collapsed lines
    // Copyright 2021 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
    //
    // https://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.
    using System;
    using System.Collections.Generic;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Geocoding;
    using Esri.ArcGISRuntime.UI;
    using System.Drawing;
    using System.Linq;
    using System.Threading.Tasks;
    namespace SearchForAnAddress
    {
    class MapViewModel : INotifyPropertyChanged
    {
    public MapViewModel()
    {
    SetupMap();
    }
    public event PropertyChangedEventHandler? PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
    var propertyChangedHandler = PropertyChanged;
    if (propertyChangedHandler != null)
    propertyChangedHandler(this, new PropertyChangedEventArgs(propertyName));
    }
    private Map? _map;
    public Map? Map
    {
    get { return _map; }
    set
    {
    _map = value;
    OnPropertyChanged();
    }
    }
    private GraphicsOverlayCollection? _graphicsOverlayCollection;
    public GraphicsOverlayCollection? GraphicsOverlays
    {
    get { return _graphicsOverlayCollection; }
    set
    {
    _graphicsOverlayCollection = value;
    OnPropertyChanged();
    }
    }
    private void SetupMap()
    {
    // Create a new map with a 'topographic vector' basemap.
    Map map = new Map(BasemapStyle.ArcGISTopographic);
    // Set the initial viewpoint around the Santa Monica Mountains in California.
    var mapCenterPoint = new MapPoint(-118.805, 34.027, SpatialReferences.Wgs84);
    map.InitialViewpoint = new Viewpoint(mapCenterPoint, 100000);
    // Set the view model "Map" property.
    this.Map = map;
    // Set the view model "GraphicsOverlays" property.
    GraphicsOverlay overlay = new GraphicsOverlay();
    GraphicsOverlayCollection overlayCollection = new GraphicsOverlayCollection
    {
    overlay
    };
    this.GraphicsOverlays = overlayCollection;
    }
    public async Task<MapPoint?> SearchAddress(string address, SpatialReference spatialReference)
    {
    MapPoint? addressLocation = null;
    try
    {
    // Get the first graphics overlay from the GraphicsOverlays and remove any previous result graphics.
    GraphicsOverlay? graphicsOverlay = this.GraphicsOverlays?.FirstOrDefault();
    graphicsOverlay?.Graphics.Clear();
    // Create a locator task.
    LocatorTask locatorTask = new LocatorTask(new Uri("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"));
    // Define geocode parameters: limit the results to one and get all attributes.
    GeocodeParameters geocodeParameters = new GeocodeParameters();
    geocodeParameters.ResultAttributeNames.Add("*");
    geocodeParameters.MaxResults = 1;
    geocodeParameters.OutputSpatialReference = spatialReference;
    13 collapsed lines
    }
    catch (Exception ex)
    {
    System.Windows.MessageBox.Show("Couldn't find address: " + ex.Message);
    }
    // Return the location of the geocode result.
    return addressLocation;
    }
    }
    }
  6. To find the location for the provided address, call LocatorTask.GeocodeAsync, providing the address string and geocodeParameters. The result is a read-only list of GeocodeResult objects. In this tutorial, either one or zero results will be returned, as the maximum results parameter was set to 1.

    103 collapsed lines
    // Copyright 2021 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
    //
    // https://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.
    using System;
    using System.Collections.Generic;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Geocoding;
    using Esri.ArcGISRuntime.UI;
    using System.Drawing;
    using System.Linq;
    using System.Threading.Tasks;
    namespace SearchForAnAddress
    {
    class MapViewModel : INotifyPropertyChanged
    {
    public MapViewModel()
    {
    SetupMap();
    }
    public event PropertyChangedEventHandler? PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
    var propertyChangedHandler = PropertyChanged;
    if (propertyChangedHandler != null)
    propertyChangedHandler(this, new PropertyChangedEventArgs(propertyName));
    }
    private Map? _map;
    public Map? Map
    {
    get { return _map; }
    set
    {
    _map = value;
    OnPropertyChanged();
    }
    }
    private GraphicsOverlayCollection? _graphicsOverlayCollection;
    public GraphicsOverlayCollection? GraphicsOverlays
    {
    get { return _graphicsOverlayCollection; }
    set
    {
    _graphicsOverlayCollection = value;
    OnPropertyChanged();
    }
    }
    private void SetupMap()
    {
    // Create a new map with a 'topographic vector' basemap.
    Map map = new Map(BasemapStyle.ArcGISTopographic);
    // Set the initial viewpoint around the Santa Monica Mountains in California.
    var mapCenterPoint = new MapPoint(-118.805, 34.027, SpatialReferences.Wgs84);
    map.InitialViewpoint = new Viewpoint(mapCenterPoint, 100000);
    // Set the view model "Map" property.
    this.Map = map;
    // Set the view model "GraphicsOverlays" property.
    GraphicsOverlay overlay = new GraphicsOverlay();
    GraphicsOverlayCollection overlayCollection = new GraphicsOverlayCollection
    {
    overlay
    };
    this.GraphicsOverlays = overlayCollection;
    }
    public async Task<MapPoint?> SearchAddress(string address, SpatialReference spatialReference)
    {
    MapPoint? addressLocation = null;
    try
    {
    // Get the first graphics overlay from the GraphicsOverlays and remove any previous result graphics.
    GraphicsOverlay? graphicsOverlay = this.GraphicsOverlays?.FirstOrDefault();
    graphicsOverlay?.Graphics.Clear();
    // Create a locator task.
    LocatorTask locatorTask = new LocatorTask(new Uri("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"));
    // Define geocode parameters: limit the results to one and get all attributes.
    GeocodeParameters geocodeParameters = new GeocodeParameters();
    geocodeParameters.ResultAttributeNames.Add("*");
    geocodeParameters.MaxResults = 1;
    geocodeParameters.OutputSpatialReference = spatialReference;
    // Geocode the address string and get the first (only) result.
    IReadOnlyList<GeocodeResult> results = await locatorTask.GeocodeAsync(address, geocodeParameters);
    GeocodeResult? geocodeResult = results[0];
    if(geocodeResult == null) { throw new Exception("No matches found."); }
    13 collapsed lines
    }
    catch (Exception ex)
    {
    System.Windows.MessageBox.Show("Couldn't find address: " + ex.Message);
    }
    // Return the location of the geocode result.
    return addressLocation;
    }
    }
    }

Display the result

The geocode result can be displayed by adding a graphic A graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. Learn more to the map view’s graphics overlay A graphics overlay is a client-side, temporary container of graphics to display on a map view or scene view. Learn more .

  1. If a result was found, create two Graphic objects and add them to the graphicsOverlay. Create a graphic to display the geocode result’s location, and another to show the geocode result’s label text (the located address).

    109 collapsed lines
    // Copyright 2021 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
    //
    // https://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.
    using System;
    using System.Collections.Generic;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Geocoding;
    using Esri.ArcGISRuntime.UI;
    using System.Drawing;
    using System.Linq;
    using System.Threading.Tasks;
    namespace SearchForAnAddress
    {
    class MapViewModel : INotifyPropertyChanged
    {
    public MapViewModel()
    {
    SetupMap();
    }
    public event PropertyChangedEventHandler? PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
    var propertyChangedHandler = PropertyChanged;
    if (propertyChangedHandler != null)
    propertyChangedHandler(this, new PropertyChangedEventArgs(propertyName));
    }
    private Map? _map;
    public Map? Map
    {
    get { return _map; }
    set
    {
    _map = value;
    OnPropertyChanged();
    }
    }
    private GraphicsOverlayCollection? _graphicsOverlayCollection;
    public GraphicsOverlayCollection? GraphicsOverlays
    {
    get { return _graphicsOverlayCollection; }
    set
    {
    _graphicsOverlayCollection = value;
    OnPropertyChanged();
    }
    }
    private void SetupMap()
    {
    // Create a new map with a 'topographic vector' basemap.
    Map map = new Map(BasemapStyle.ArcGISTopographic);
    // Set the initial viewpoint around the Santa Monica Mountains in California.
    var mapCenterPoint = new MapPoint(-118.805, 34.027, SpatialReferences.Wgs84);
    map.InitialViewpoint = new Viewpoint(mapCenterPoint, 100000);
    // Set the view model "Map" property.
    this.Map = map;
    // Set the view model "GraphicsOverlays" property.
    GraphicsOverlay overlay = new GraphicsOverlay();
    GraphicsOverlayCollection overlayCollection = new GraphicsOverlayCollection
    {
    overlay
    };
    this.GraphicsOverlays = overlayCollection;
    }
    public async Task<MapPoint?> SearchAddress(string address, SpatialReference spatialReference)
    {
    MapPoint? addressLocation = null;
    try
    {
    // Get the first graphics overlay from the GraphicsOverlays and remove any previous result graphics.
    GraphicsOverlay? graphicsOverlay = this.GraphicsOverlays?.FirstOrDefault();
    graphicsOverlay?.Graphics.Clear();
    // Create a locator task.
    LocatorTask locatorTask = new LocatorTask(new Uri("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"));
    // Define geocode parameters: limit the results to one and get all attributes.
    GeocodeParameters geocodeParameters = new GeocodeParameters();
    geocodeParameters.ResultAttributeNames.Add("*");
    geocodeParameters.MaxResults = 1;
    geocodeParameters.OutputSpatialReference = spatialReference;
    // Geocode the address string and get the first (only) result.
    IReadOnlyList<GeocodeResult> results = await locatorTask.GeocodeAsync(address, geocodeParameters);
    GeocodeResult? geocodeResult = results[0];
    if(geocodeResult == null) { throw new Exception("No matches found."); }
    // Create a graphic to display the result location.
    SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Square, Color.Blue, 12);
    Graphic markerGraphic = new Graphic(geocodeResult.DisplayLocation, geocodeResult.Attributes, markerSymbol);
    // Create a graphic to display the result address label.
    TextSymbol textSymbol = new TextSymbol(geocodeResult.Label, Color.Red, 18, HorizontalAlignment.Center, VerticalAlignment.Bottom);
    Graphic textGraphic = new Graphic(geocodeResult.DisplayLocation, textSymbol);
    // Add the location and label graphics to the graphics overlay.
    graphicsOverlay?.Graphics.Add(markerGraphic);
    graphicsOverlay?.Graphics.Add(textGraphic);
    13 collapsed lines
    }
    catch (Exception ex)
    {
    System.Windows.MessageBox.Show("Couldn't find address: " + ex.Message);
    }
    // Return the location of the geocode result.
    return addressLocation;
    }
    }
    }
  2. Set addressLocation with the location of the result to return it from the function. The calling function will use this MapPoint to pan the display to the result location.

    122 collapsed lines
    // Copyright 2021 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
    //
    // https://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.
    using System;
    using System.Collections.Generic;
    using Esri.ArcGISRuntime.Geometry;
    using Esri.ArcGISRuntime.Mapping;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Esri.ArcGISRuntime.Symbology;
    using Esri.ArcGISRuntime.Tasks.Geocoding;
    using Esri.ArcGISRuntime.UI;
    using System.Drawing;
    using System.Linq;
    using System.Threading.Tasks;
    namespace SearchForAnAddress
    {
    class MapViewModel : INotifyPropertyChanged
    {
    public MapViewModel()
    {
    SetupMap();
    }
    public event PropertyChangedEventHandler? PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
    var propertyChangedHandler = PropertyChanged;
    if (propertyChangedHandler != null)
    propertyChangedHandler(this, new PropertyChangedEventArgs(propertyName));
    }
    private Map? _map;
    public Map? Map
    {
    get { return _map; }
    set
    {
    _map = value;
    OnPropertyChanged();
    }
    }
    private GraphicsOverlayCollection? _graphicsOverlayCollection;
    public GraphicsOverlayCollection? GraphicsOverlays
    {
    get { return _graphicsOverlayCollection; }
    set
    {
    _graphicsOverlayCollection = value;
    OnPropertyChanged();
    }
    }
    private void SetupMap()
    {
    // Create a new map with a 'topographic vector' basemap.
    Map map = new Map(BasemapStyle.ArcGISTopographic);
    // Set the initial viewpoint around the Santa Monica Mountains in California.
    var mapCenterPoint = new MapPoint(-118.805, 34.027, SpatialReferences.Wgs84);
    map.InitialViewpoint = new Viewpoint(mapCenterPoint, 100000);
    // Set the view model "Map" property.
    this.Map = map;
    // Set the view model "GraphicsOverlays" property.
    GraphicsOverlay overlay = new GraphicsOverlay();
    GraphicsOverlayCollection overlayCollection = new GraphicsOverlayCollection
    {
    overlay
    };
    this.GraphicsOverlays = overlayCollection;
    }
    public async Task<MapPoint?> SearchAddress(string address, SpatialReference spatialReference)
    {
    MapPoint? addressLocation = null;
    try
    {
    // Get the first graphics overlay from the GraphicsOverlays and remove any previous result graphics.
    GraphicsOverlay? graphicsOverlay = this.GraphicsOverlays?.FirstOrDefault();
    graphicsOverlay?.Graphics.Clear();
    // Create a locator task.
    LocatorTask locatorTask = new LocatorTask(new Uri("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"));
    // Define geocode parameters: limit the results to one and get all attributes.
    GeocodeParameters geocodeParameters = new GeocodeParameters();
    geocodeParameters.ResultAttributeNames.Add("*");
    geocodeParameters.MaxResults = 1;
    geocodeParameters.OutputSpatialReference = spatialReference;
    // Geocode the address string and get the first (only) result.
    IReadOnlyList<GeocodeResult> results = await locatorTask.GeocodeAsync(address, geocodeParameters);
    GeocodeResult? geocodeResult = results[0];
    if(geocodeResult == null) { throw new Exception("No matches found."); }
    // Create a graphic to display the result location.
    SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Square, Color.Blue, 12);
    Graphic markerGraphic = new Graphic(geocodeResult.DisplayLocation, geocodeResult.Attributes, markerSymbol);
    // Create a graphic to display the result address label.
    TextSymbol textSymbol = new TextSymbol(geocodeResult.Label, Color.Red, 18, HorizontalAlignment.Center, VerticalAlignment.Bottom);
    Graphic textGraphic = new Graphic(geocodeResult.DisplayLocation, textSymbol);
    // Add the location and label graphics to the graphics overlay.
    graphicsOverlay?.Graphics.Add(markerGraphic);
    graphicsOverlay?.Graphics.Add(textGraphic);
    // Set the address location to return from the function.
    addressLocation = geocodeResult?.DisplayLocation;
    13 collapsed lines
    }
    catch (Exception ex)
    {
    System.Windows.MessageBox.Show("Couldn't find address: " + ex.Message);
    }
    // Return the location of the geocode result.
    return addressLocation;
    }
    }
    }

Search for an address when the button is clicked

  1. In the Visual Studio > Solution Explorer, double-click MainWindow.xaml.cs to open the file.

  2. Add a handler for the SearchAddressButton click event.

    37 collapsed lines
    // Copyright 2021 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
    //
    // https://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.
    using Esri.ArcGISRuntime.Geometry;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    namespace SearchForAnAddress
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
    public MainWindow()
    {
    InitializeComponent();
    }
    private async void SearchAddressButton_Click(object sender, RoutedEventArgs e)
    {
    }
    3 collapsed lines
    }
    }
  3. Find MapViewModel from the page resources.

    42 collapsed lines
    // Copyright 2021 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
    //
    // https://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.
    using Esri.ArcGISRuntime.Geometry;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    namespace SearchForAnAddress
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
    public MainWindow()
    {
    InitializeComponent();
    }
    private async void SearchAddressButton_Click(object sender, RoutedEventArgs e)
    {
    // Get the MapViewModel from the page (defined as a static resource).
    var viewModel = (MapViewModel)FindResource("MapViewModel");
    if(viewModel == null) { return; }
    }
    3 collapsed lines
    }
    }
  4. Call the SearchAddress function on the MapViewModel and store the returned MapPoint.

    42 collapsed lines
    // Copyright 2021 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
    //
    // https://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.
    using Esri.ArcGISRuntime.Geometry;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    namespace SearchForAnAddress
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
    public MainWindow()
    {
    InitializeComponent();
    }
    private async void SearchAddressButton_Click(object sender, RoutedEventArgs e)
    {
    // Get the MapViewModel from the page (defined as a static resource).
    var viewModel = (MapViewModel)FindResource("MapViewModel");
    if(viewModel == null) { return; }
    // Call SearchAddress on the view model, pass the address text and the map view's spatial reference.
    var spatialReference = MainMapView.SpatialReference;
    if(spatialReference== null) { return; }
    MapPoint? addressPoint = await viewModel.SearchAddress(AddressTextBox.Text, spatialReference);
    }
    3 collapsed lines
    }
    }
  5. If a map point is returned, center the MapView on the address result.

    42 collapsed lines
    // Copyright 2021 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
    //
    // https://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.
    using Esri.ArcGISRuntime.Geometry;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    namespace SearchForAnAddress
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
    public MainWindow()
    {
    InitializeComponent();
    }
    private async void SearchAddressButton_Click(object sender, RoutedEventArgs e)
    {
    // Get the MapViewModel from the page (defined as a static resource).
    var viewModel = (MapViewModel)FindResource("MapViewModel");
    if(viewModel == null) { return; }
    // Call SearchAddress on the view model, pass the address text and the map view's spatial reference.
    var spatialReference = MainMapView.SpatialReference;
    if(spatialReference== null) { return; }
    MapPoint? addressPoint = await viewModel.SearchAddress(AddressTextBox.Text, spatialReference);
    // If a result was found, center the display on it.
    if (addressPoint != null)
    {
    await MainMapView.SetViewpointCenterAsync(addressPoint);
    }
    }
    3 collapsed lines
    }
    }
  6. Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app. If your app uses user authentication, enter your ArcGIS Online credentials when prompted.

You will see the address search controls at the top center of the map. To search for an address, enter an address and then click Search. If a result is found, the map will pan to the location and label a graphic for that address.

Alternatively, you can download the tutorial solution, as follows.

Option 2: Download the solution

  1. Click the Download solution link in the right-hand panel of the page.

  2. Unzip the file to a location on your machine.

  3. Open the .sln file in Visual Studio.

Since the downloaded solution does not contain authentication credentials, you must add the developer credentials that you created in the Set up authentication section.

Set developer credentials in the solution

To allow your app users to access ArcGIS location services ArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. Learn more , use the developer credentials that you created in the Set up authentication step to authenticate requests for resources.

  1. In Visual Studio, in the Solution Explorer, click App.xaml.cs to open the file.

  2. Set the ArcGISEnvironment.ApiKey property with your API key access token.

    App.xaml.cs
    protected override void OnStartup(StartupEventArgs e)
    {
    base.OnStartup(e);
    // Set the access token for ArcGIS Maps SDK for .NET.
    Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey = "YOUR_ACCESS_TOKEN";
    // Call a function to set up the AuthenticationManager for OAuth.
    UserAuth.ArcGISLoginPrompt.RegisterOAuthConfig();
    }
  3. Remove the code that sets up user authentication.

    App.xaml.cs
    protected override void OnStartup(StartupEventArgs e)
    {
    base.OnStartup(e);
    // Set the access token for ArcGIS Maps SDK for .NET.
    Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey = "YOUR_ACCESS_TOKEN";
    // Call a function to set up the AuthenticationManager for OAuth.
    UserAuth.ArcGISLoginPrompt.RegisterOAuthConfig();
    }

Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

Run the solution

Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app. If your app uses user authentication, enter your ArcGIS Online credentials when prompted.

You will see the address search controls at the top center of the map. To search for an address, enter an address and then click Search. If a result is found, the map will pan to the location and label a graphic for that address.

What’s next?

Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials: