Click or drag to resize

GeocodeParameters Class

A class that describes geocode method parameters.
Inheritance Hierarchy
SystemObject
  Esri.ArcGISRuntime.Tasks.GeocodingGeocodeParameters

Namespace:  Esri.ArcGISRuntime.Tasks.Geocoding
Assembly:  Esri.ArcGISRuntime (in Esri.ArcGISRuntime.dll) Version: 100.11.0
Syntax
public sealed class GeocodeParameters

The GeocodeParameters type exposes the following members.

Constructors
  NameDescription
Public methodCode exampleGeocodeParameters
Initializes a new instance of the GeocodeParameters class.
Top
Properties
  NameDescription
Public propertyCategories
Gets a mutable list of categories from which addresses should be returned.
Public propertyCountryCode
Gets or sets a value representing the country. Providing this value increases geocoding speed. Acceptable values include the full country name, the ISO 3166-1 2-digit country code, or the ISO 3166-1 3-digit country code. A list of supported countries and codes is available here.
Public propertyIsForStorage
Gets or sets the for storage indicator.
Public propertyMaxResults
Gets or sets the maximum number of candidates to be returned by a search, up to the maximum number allowed by the service. By default all found candidates will be returned. The world geocoding service allows up to 20 candidates to be returned for a single request.
Public propertyMinScore
Gets or sets the minimum score value.
Public propertyOutputLanguage
Gets or sets the output language.
Public propertyOutputSpatialReference
Gets or sets the spatial reference of the returned locations. If not specified, the coordinates of the returned locations will be in the same spatial reference as the service.
Public propertyCode examplePreferredSearchLocation
Gets or sets the preferred search location.
Public propertyResultAttributeNames
Gets a mutable list of attribute names.
Public propertyCode exampleSearchArea
Gets or sets the geographic area for which searching for locations will be limited to.
Top
Remarks
Class holding the parameters to support the geocode operation for a geocoding service or local locator.
Examples

WPF

Example Name: FindPlace

Find places of interest near a location or within a specific area.

Code example screen shot.

C#
// Copyright 2018 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.

using Esri.ArcGISRuntime.Data;
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 System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.WPF.Samples.FindPlace
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Find place",
        category: "Search",
        description: "Find places of interest near a location or within a specific area.",
        instructions: "Choose a type of place in the first field and an area to search within in the second field. Click the Search button to show the results of the query on the map. Click on a result pin to show its name and address. If you pan away from the result area, a \"Redo search in this area\" button will appear. Click it to query again for the currently viewed area on the map.",
        tags: new[] { "POI", "businesses", "geocode", "locations", "locator", "places of interest", "point of interest", "search", "suggestions" })]
    [ArcGISRuntime.Samples.Shared.Attributes.EmbeddedResource(@"PictureMarkerSymbols\pin_star_blue.png")]
    public partial class FindPlace
    {
        // Flag used to help prevent search completion from competing with user input.
        private bool _waitFlag;

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

        // Service Uri to be provided to the LocatorTask (geocoder).
        private readonly Uri _serviceUri =
            new Uri("https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer");

        public FindPlace()
        {
            InitializeComponent();

            // Setup the control references and execute initialization.
            Initialize();
        }

        private async void Initialize()
        {
            // Add event handler for when this sample is unloaded.
            Unloaded += SampleUnloaded;

            // Show a map with a streets basemap.
            MyMapView.Map = new Map(BasemapStyle.ArcGISStreets);

            // Subscribe to location changed events (to support zooming to current location).
            MyMapView.LocationDisplay.LocationChanged += LocationDisplay_LocationChanged;

            // Enable location display.
            MyMapView.LocationDisplay.IsEnabled = true;

            // Enable tap-for-info pattern on results.
            MyMapView.GeoViewTapped += MyMapView_GeoViewTapped;

            // Initialize the LocatorTask with the provided service Uri.
            _geocoder = await LocatorTask.CreateAsync(_serviceUri);

            // Enable all controls now that the locator task is ready.
            MySearchBox.IsEnabled = true;
            MyLocationBox.IsEnabled = true;
            MySearchButton.IsEnabled = true;
            MySearchRestrictedButton.IsEnabled = true;
        }

        private void LocationDisplay_LocationChanged(object sender, Esri.ArcGISRuntime.Location.Location e)
        {
            // Return if position is null; event is called with null position when location display is turned on.
            if (e.Position == null)
            {
                return;
            }

            // Unsubscribe from the event (only want to zoom once).
            ((LocationDisplay)sender).LocationChanged -= LocationDisplay_LocationChanged;

            // Needed because the event is called from a background (non-UI) thread and this code is manipulating UI.
            Dispatcher.Invoke(() =>
            {
                // Zoom to the location.
                MyMapView.SetViewpointAsync(new Viewpoint(e.Position, 100000));
            });
        }

        /// <summary>
        /// Gets the map point corresponding to the text in the location textbox.
        /// If the text is 'Current Location', the returned map point will be the device's location.
        /// </summary>
        private async Task<MapPoint> GetSearchMapPoint(string locationText)
        {
            // Get the map point for the search text.
            if (locationText != "Current Location")
            {
                // Geocode the location.
                IReadOnlyList<GeocodeResult> locations = await _geocoder.GeocodeAsync(locationText);

                // return if there are no results.
                if (!locations.Any())
                {
                    return null;
                }

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

                // Return the map point.
                return result.DisplayLocation;
            }
            else
            {
                // Get the current device location.
                return MyMapView.LocationDisplay.Location.Position;
            }
        }

        /// <summary>
        /// Runs a search and populates the map with results based on the provided information.
        /// </summary>
        /// <param name="enteredText">Results to search for.</param>
        /// <param name="locationText">Location around which to find results.</param>
        /// <param name="restrictToExtent">If true, limits results to only those that are within the current extent.</param>
        private async void UpdateSearch(string enteredText, string locationText, bool restrictToExtent = false)
        {
            // Clear any existing markers.
            MyMapView.GraphicsOverlays.Clear();

            // Return gracefully if the textbox is empty or the geocoder isn't ready.
            if (String.IsNullOrWhiteSpace(enteredText) || _geocoder == null)
            {
                return;
            }

            // Create the geocode parameters.
            GeocodeParameters parameters = new GeocodeParameters();

            // Get the MapPoint for the current search location.
            MapPoint searchLocation = await GetSearchMapPoint(locationText);

            // Update the geocode parameters if the map point is not null.
            if (searchLocation != null)
            {
                parameters.PreferredSearchLocation = searchLocation;
            }

            // Update the search area if desired.
            if (restrictToExtent)
            {
                // Update the search parameters with the current map extent.
                parameters.SearchArea = MyMapView.VisibleArea;
            }

            // Show the progress bar.
            MyProgressBar.Visibility = Visibility.Visible;

            // Get the location information.
            IReadOnlyList<GeocodeResult> locations = await _geocoder.GeocodeAsync(enteredText, parameters);

            // Stop gracefully and show a message if the geocoder does not return a result.
            if (locations.Count < 1)
            {
                MyProgressBar.Visibility = Visibility.Collapsed; // 1. Hide the progress bar.
                MessageBox.Show("No results found"); // 2. Show a message.
                return; // 3. Stop.
            }

            // Create the GraphicsOverlay so that results can be drawn on the map.
            GraphicsOverlay resultOverlay = new GraphicsOverlay();

            // Add each address to the map.
            foreach (GeocodeResult location in locations)
            {
                // Get the Graphic to display.
                Graphic point = await GraphicForPoint(location.DisplayLocation);

                // Add the specific result data to the point.
                point.Attributes["Match_Title"] = location.Label;

                // Get the address for the point.
                IReadOnlyList<GeocodeResult> addresses = await _geocoder.ReverseGeocodeAsync(location.DisplayLocation);

                // Add the first suitable address if possible.
                if (addresses.Any())
                {
                    point.Attributes["Match_Address"] = addresses[0].Label;
                }

                // Add the Graphic to the GraphicsOverlay.
                resultOverlay.Graphics.Add(point);
            }

            // Hide the progress bar.
            MyProgressBar.Visibility = Visibility.Collapsed;

            // Add the GraphicsOverlay to the MapView.
            MyMapView.GraphicsOverlays.Add(resultOverlay);

            // Update the map viewpoint.
            await MyMapView.SetViewpointGeometryAsync(resultOverlay.Extent, 50);
        }

        /// <summary>
        /// Creates and returns a Graphic associated with the given MapPoint.
        /// </summary>
        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(
                "ArcGISRuntime.Resources.PictureMarkerSymbols.pin_star_blue.png");

            // Create new symbol using asynchronous factory method from stream.
            PictureMarkerSymbol pinSymbol = await PictureMarkerSymbol.CreateAsync(resourceStream);
            pinSymbol.Width = 60;
            pinSymbol.Height = 60;
            // The symbol is a pin; centering it on the point is incorrect.
            // The values below center the pin and offset it so that the pinpoint is accurate.
            pinSymbol.LeaderOffsetX = 30;
            pinSymbol.OffsetY = 14;
            return new Graphic(point, pinSymbol);
        }

        /// <summary>
        /// Shows a callout for any tapped graphics.
        /// </summary>
        private async void MyMapView_GeoViewTapped(object sender, GeoViewInputEventArgs e)
        {
            // Search for the graphics underneath the user's tap.
            IReadOnlyList<IdentifyGraphicsOverlayResult> results =
                await MyMapView.IdentifyGraphicsOverlaysAsync(e.Position, 12, false);

            // Clear callouts and return if there was no result.
            if (results.Count < 1 || results.First().Graphics.Count < 1)
            {
                MyMapView.DismissCallout();
                return;
            }

            // Get the first graphic from the first result.
            Graphic matchingGraphic = results.First().Graphics.First();

            // Get the title; manually added to the point's attributes in UpdateSearch.
            string title = matchingGraphic.Attributes["Match_Title"].ToString();

            // Get the address; manually added to the point's attributes in UpdateSearch.
            string address = matchingGraphic.Attributes["Match_Address"].ToString();

            // Define the callout.
            CalloutDefinition calloutBody = new CalloutDefinition(title, address);

            // Show the callout on the map at the tapped location.
            MyMapView.ShowCalloutAt(e.Location, calloutBody);
        }

        /// <summary>
        /// Returns a list of suggestions based on the input search text and limited by the specified parameters.
        /// </summary>
        /// <param name="searchText">Text to get suggestions for.</param>
        /// <param name="location">Location around which to look for suggestions.</param>
        /// <param name="poiOnly">If true, restricts suggestions to only Points of Interest (e.g. businesses, parks),
        /// rather than all matching results.</param>
        /// <returns>List of suggestions as strings.</returns>
        private async Task<List<string>> GetSuggestResults(string searchText, string location = "",
            bool poiOnly = false)
        {
            // Quit if string is null, empty, or whitespace.
            if (String.IsNullOrWhiteSpace(searchText))
            {
                return new List<string>();
            }

            // Quit if the geocoder isn't ready.
            if (_geocoder == null)
            {
                return new List<string>();
            }

            // Create geocode parameters.
            SuggestParameters parameters = new SuggestParameters();

            // Restrict suggestions to points of interest if desired.
            if (poiOnly)
            {
                parameters.Categories.Add("POI");
            }

            // Set the location for the suggest parameters.
            if (!String.IsNullOrWhiteSpace(location))
            {
                // Get the MapPoint for the current search location.
                MapPoint searchLocation = await GetSearchMapPoint(location);

                // Update the geocode parameters if the map point is not null.
                if (searchLocation != null)
                {
                    parameters.PreferredSearchLocation = searchLocation;
                }
            }

            // Get the updated results from the query so far.
            IReadOnlyList<SuggestResult> results = await _geocoder.SuggestAsync(searchText, parameters);

            // Convert the list into a list of strings (corresponding to the label property on each result) and return.
            return results.Select(result => result.Label).ToList();
        }

        /// <summary>
        /// Method used to keep the suggestions up-to-date for the search box.
        /// </summary>
        private async void MySearchBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // Don't update results immediately; makes search-as-you-type more comfortable
            if (_waitFlag)
            {
                return;
            }

            _waitFlag = true;
            await Task.Delay(150);
            _waitFlag = false;

            // Dismiss callout, if any.
            MyMapView.DismissCallout();

            // Get the current text.
            string searchText = MySearchBox.Text;

            // Get the current search location.
            string locationText = MyLocationBox.Text;

            // Convert the list into a usable format for the suggest box.
            List<string> results = await GetSuggestResults(searchText, locationText, true);

            // Quit if there are no results.
            if (!results.Any())
            {
                return;
            }

            // Update the list of options.
            MySearchBox.ItemsSource = results;
        }

        /// <summary>
        /// Method used to keep the suggestions up-to-date for the location box.
        /// </summary>
        private async void MyLocationBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // Don't update results immediately; makes search-as-you-type more comfortable
            if (_waitFlag)
            {
                return;
            }

            _waitFlag = true;
            await Task.Delay(150);
            _waitFlag = false;

            // Dismiss callout, if any.
            MyMapView.DismissCallout();

            // Get the current text.
            string searchText = MyLocationBox.Text;

            // Get the results.
            List<string> results = await GetSuggestResults(searchText);

            // Quit if there are no results.
            if (!results.Any())
            {
                return;
            }

            // Add a 'current location' option to the list.
            results.Insert(0, "Current Location");

            // Update the list of options.
            MyLocationBox.ItemsSource = results;
        }

        /// <summary>
        /// Method called to start a search that is restricted to results within the current extent.
        /// </summary>
        private void MySearchRestrictedButton_Click(object sender, RoutedEventArgs e)
        {
            // Dismiss callout, if any.
            MyMapView.DismissCallout();

            // Get the search text.
            string searchText = MySearchBox.Text;

            // Get the location text.
            string locationText = MyLocationBox.Text;

            // Run the search.
            UpdateSearch(searchText, locationText, true);
        }

        /// <summary>
        /// Method called to start an unrestricted search.
        /// </summary>
        private void MySearchButton_Click(object sender, RoutedEventArgs e)
        {
            // Dismiss callout, if any.
            MyMapView.DismissCallout();

            // Get the search text.
            string searchText = MySearchBox.Text;

            // Get the location text.
            string locationText = MyLocationBox.Text;

            // Run the search.
            UpdateSearch(searchText, locationText);
        }

        private void SampleUnloaded(object sender, RoutedEventArgs e)
        {
            // Stop the location data source.
            MyMapView.LocationDisplay?.DataSource?.StopAsync();
        }
    }
}
XAML
<UserControl x:Class="ArcGISRuntime.WPF.Samples.FindPlace.FindPlace"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">
    <Grid>
        <esri:MapView x:Name="MyMapView" />
        <Border Style="{StaticResource BorderStyle}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="2*" />
                </Grid.ColumnDefinitions>
                <TextBlock
                    Text="Enter a search term and a search location. Tap 'Search all' to find all results near that location. Tap 'Search in view' to restrict the search to results within the current visible extent."
                    Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
                    TextAlignment="Left" TextWrapping="Wrap" FontWeight="SemiBold" />
                <TextBlock Text="Search"
                           Grid.Row="1" Grid.Column="0"
                           Margin="0,5,5,0" />
                <TextBlock Text="Location"
                           Grid.Row="2" Grid.Column="0"
                           Margin="0,5,5,0" />
                <ComboBox x:Name="MySearchBox"
                          Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"
                          Text="Coffee"
                          Margin="0,5,0,0"
                          IsEnabled="False" IsEditable="True"
                          TextBoxBase.TextChanged="MySearchBox_TextChanged" />
                <ComboBox x:Name="MyLocationBox"
                          Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2"
                          Text="Current Location"
                          Margin="0,5,0,0"
                          IsEditable="True" IsEnabled="False"
                          TextBoxBase.TextChanged="MyLocationBox_TextChanged" />
                <Button x:Name="MySearchButton"
                        Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"
                        Content="Search all"
                        IsEnabled="False"
                        Margin="0,5,5,0"
                        Click="MySearchButton_Click" />
                <Button x:Name="MySearchRestrictedButton"
                        Grid.Row="3" Grid.Column="2"
                        Content="Search in view"
                        IsEnabled="False"
                        Margin="5,5,0,0"
                        Click="MySearchRestrictedButton_Click" />
                <ProgressBar x:Name="MyProgressBar"
                             Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="3"
                             Visibility="Collapsed" IsIndeterminate="True"
                             Margin="0,5,0,0" Height="20" />
            </Grid>
        </Border>
    </Grid>
</UserControl>
See Also
Additional Examples
Hyperlink to ExampleDescription
FindPlaceFind places of interest near a location or within a specific area.