Learn how to find an address or place with a search bar and the Geocoding service.
Geocoding is the process of converting address or place text into a location. The Geocoding service can search for an address or a place and perform reverse geocoding.
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, you should:
Have an ArcGIS account and an API key to access ArcGIS services. If you don't have an account, sign up for free.
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.
Steps
Open a Visual Studio solution
To start the tutorial, complete the Display a map tutorial or download and unzip the solution.
Open the .sln file in Visual Studio.
If you downloaded the solution project, set your API key.
An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.
If necessary, set the API Key.
Go to your developer dashboard to get your API key.
For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.
In Visual Studio, in the Solution Explorer, click App.xaml.cs.
In the App class, add an override for the OnStartup() function to set the ApiKey property on ArcGISRuntimeEnvironment.
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
publicpartialclassApp : Application {
protectedoverridevoidOnStartup(StartupEventArgs e) {
base.OnStartup(e);
// Note: it is not best practice to store API keys in source code.// The API key is referenced here for the convenience of this tutorial. Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey = "YOUR_API_KEY";
}
}
}
If you are developing with Visual Studio for Windows, ArcGIS Maps SDK for .NET provides a set of project templates for each supported .NET platform. These templates provide all of the code needed for a basic Model-View-ViewModel (MVVM) app. Install the ArcGIS Maps SDK for .NET Visual Studio Extension to add the templates to Visual Studio (Windows only). See Install and set up for details.
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.
The tutorial instructions and code use the name SearchForAnAddress for the solution, project, and namespace. You can choose any name you like, but it should be the same for each of these.
Update the name for the solution and the project.
In Visual Studio, in the Solution Explorer, right-click the solution name and choose Rename. Provide the new name for your solution.
In the Solution Explorer, right-click the project name and choose Rename. Provide the new name for your project.
Rename the namespace used by classes in the project.
In the Solution Explorer, expand the project node.
Double-click MapViewModel.cs in the Solution Explorer to open the file.
In the MapViewModel class, double-click the namespace name (DisplayAMap) to select it, and then right-click and choose Rename....
Provide the new name for the namespace.
Click Apply in the Rename: DisplayAMap window that appears in the upper-right of the code window. This will rename the namespace throughout your project.
Build the project.
Choose Build > Build solution (or press <F6>).
Add a graphics overlay
A graphics overlay is a container for graphics. 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. They do not persist when you save the map.
In the Visual Studio > Solution Explorer, double-click MapViewModel.cs to open the file.
The project uses the Model-View-ViewModel (MVVM) design pattern to separate the application logic (view model) from the user interface (view). MapViewModel.cs contains the view model class for the application, called MapViewModel. See the Microsoft documentation for more information about the Model-View-ViewModel pattern.
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.
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).
In the Visual Studio > Solution Explorer, double-click MainWindow.xaml to open the file.
Use data binding to bind the GraphicsOverlays property of the MapViewModel to the MapView control.
Data binding and the Model-View-ViewModel (MVVM) design pattern allow you to separate the logic in your app (the view model) from the presentation layer (the view). Graphics can be added and removed from the graphics overlays in the view model, and those changes appear in the map view through data binding.
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.
In the Visual Studio > Solution Explorer, double-click MainWindow.xaml to open the file.
Add a Border above the map view that contains the address search controls.
Geocoding is implemented with a locator, typically created by referencing a geocoding service such as the Geocoding service or, for offline geocoding, by referencing locator data contained in a mobile package. 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.
In the Visual Studio > Solution Explorer, double-click MapViewModel.cs to open the file.
Keeping with the MVVM design pattern, the geocoding logic, along with any code unrelated to the user interface, is stored in the view model.
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.
Asynchronous functions generally return Task<T>, where T is the type expected by the awaited call. In this function, when an address is geocoded, a MapPoint representing the address location is returned. If geocoding fails to find a match, null is returned.
A locator task is used to find the location of an address (geocode) or to interpolate an address for a location (reverse geocode). An address includes any type of information that distinguishes a place. A locator involves finding matching locations for a given address. Reverse-geocoding is the opposite and finds the closest address for a given location.
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 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.
When geocoding an address, you can optionally provide GeocodeParameters to control certain aspects of the geocoding operation and specify the kinds of results to return from the locator task. Learn more about these parameters in the GeocodeParameters. For a list of attributes returned with geocode results, see Geocoding service output in the ArcGIS services reference.
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.
The geocode result can be displayed by adding a graphic to the map view's graphics overlay.
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).
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.
In the Visual Studio > Solution Explorer, double-click MainWindow.xaml.cs to open the file.
A view defined with XAML, such as MainWindow, is composed of two files in a Visual Studio project. The visual elements (such as map views, buttons, text boxes, and so on) are defined with XAML markup in a .xaml file. This file is often referred to as the "page". The code for the XAML elements is stored in an associated .xaml.cs file that is known as the "code-behind", since it stores the code behind the controls. Keeping with the MVVM design pattern, user interface events (such as the code that responds to a button click) are handled in the code-behind for the view.
Add a handler for the SearchAddressButton click event.
Asynchronous functions should return Task or Task<T>. For event handlers, such as the Button.Click handler, the function must return void to conform to the delegate signature. See the Microsoft documentation regarding Async return types.
Expand
Use dark colors for code blocks
Add line.Add line.Add line.Add line.
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
publicMainWindow() {
InitializeComponent();
// Create a point centered on the Santa Monica mountains in Southern California.// Longitude=118.805 degrees West, Latitude=34.027 degrees Northvar mapCenterPoint = new Esri.ArcGISRuntime.Geometry.MapPoint(-118.805, 34.027, Esri.ArcGISRuntime.Geometry.SpatialReferences.Wgs84);
// Create a viewpoint for the initial display of the map.// Use the point defined above and a scale of 1:100,000 MainMapView.SetViewpoint(new Esri.ArcGISRuntime.Mapping.Viewpoint(mapCenterPoint, 100_000));
}
privateasyncvoidSearchAddressButton_Click(object sender, RoutedEventArgs e) {
}
Call the SearchAddress function on the MapViewModel and store the returned MapPoint.
Expand
Use dark colors for code blocks
Add line.Add line.
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
privateasyncvoidSearchAddressButton_Click(object sender, RoutedEventArgs e) {
// Get the MapViewModel from the page (defined as a static resource). MapViewModel viewModel = FindResource("MapViewModel") as MapViewModel;
// Call SearchAddress on the view model, pass the address text and the map view's spatial reference. Esri.ArcGISRuntime.Geometry.MapPoint addressPoint = await viewModel.SearchAddress(AddressTextBox.Text, MainMapView.SpatialReference);
}
Expand
If a map point is returned, center the MapView on the address result.
Expand
Use dark colors for code blocks
Add line.Add line.Add line.Add line.Add line.
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
privateasyncvoidSearchAddressButton_Click(object sender, RoutedEventArgs e) {
// Get the MapViewModel from the page (defined as a static resource). MapViewModel viewModel = FindResource("MapViewModel") as MapViewModel;
// Call SearchAddress on the view model, pass the address text and the map view's spatial reference. Esri.ArcGISRuntime.Geometry.MapPoint addressPoint = await viewModel.SearchAddress(AddressTextBox.Text, MainMapView.SpatialReference);
// If a result was found, center the display on it.if (addressPoint != null)
{
await MainMapView.SetViewpointCenterAsync(addressPoint);
}
}
Expand
Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app.
You should 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.