ArcGIS Runtime API for .NET——地理编码和搜索
地理编码和搜索
使用地名或街道地址作为输入,您可以找到感兴趣的位置并在地图上与它们进行交互。 将地图上的位置与地址匹配的过程称为地理编码。 在线或本地定位器包含用于将地址,地名或类别与地理位置进行匹配的逻辑。
一.什么是地理编码?
地理编码是为地址或地名输入文本以返回地理位置和其他地址信息(完整地址)的过程。 例如,您可以对“ 1600 Pennsylvania Ave,DC”进行地理编码以返回其位置(经度-77.03654,纬度38.89767)和更完整的地址(“华盛顿特区,宾夕法尼亚州大街1600号,华盛顿特区20500”)。
可以使用地理编码来进行以下工作:
(1)查找地址或地点的位置。
(2)将不完整的地址文本转换为完整的地址。
(3)提供不完整地址或地点的候选人列表。
1.寻找地点
位置搜索(也称为兴趣点(POI)搜索)使用名称或类别来查找地理实体(例如企业,行政边界和其他要素)。 例如,可以搜索某个位置附近的餐厅,某州的县或大峡谷等土地特征。
可以执行两种搜索:本地搜索和全局搜索。 本地搜索使用位置来对附近的结果进行优先级排序,或者使用搜索范围来限制搜索区域并以定义的边界限制结果。 全局搜索是一种开放搜索,通常不局限于某个范围。
可以使用地点搜索来:
(1)查找世界各地的位置。
(2)在位置附近找到商家。
(3)按类别搜索地点,例如餐馆,加油站或学校。
(4)在地图上查找并显示地点。
2.建议
提示功能允许从地理编码应用程序中的用户输入生成逐个字符的自动完成提示。 此功能通过减少在获得建议的匹配之前需要输入的字符数来促进交互式搜索体验。
与地理编码时一样,可以传递大多数相同的参数来生成建议,例如首选的搜索位置和类别。 返回适当的自动完成建议后,建议结果可以直接传递到地理编码操作。 为了优化质量和性能,您应该传递相同的参数来建议和地理编码。
可以使用建议来:
当用户不确定确切的地名或地址时,生成更完整的输入。
尽量减少用户需要输入的字符数才能获得良好的效果。
通过将建议的结果传递给具有相同参数的地址解析来优化质量和性能。
二.反向地理编码
反向地理编码是内插街道地址或标识输入点的地名的过程。要优化搜索,可以指定一种要素类型以返回特定类型的地址,例如仅POI或地址。还可以指定返回街道地址还是屋顶位置。
地理编码服务使用位置和所有参数返回最匹配的单个地址,地点或交叉点。此反向地理编码结果可能包含许多属性,例如地名,完整地址,城市,区域和位置。
反向地理编码将始终返回最接近的结果,并且该结果可能是地址,地点或交叉点。如果您使用的定位器仅使用POI数据构建,则结果将始终是POI。如果定位器是为支持多种要素类型而构建的,则可以传递要素类型参数以确保返回的结果具有所需的类型。
可以使用反向地理编码来:
(1)获取距你当前位置最近的地址,地点或十字路口。
(2)点按地图时显示地址或地名。
(3)查找地理位置的最近地址,地点或十字路口。
三.地理编码的工作原理
地理编码是使用在线或本地定位器实现的。指定地址,输出数据字段以及可选的其他参数以优化搜索。输入的地址越完整(例如,包括邮政编码),定位器就越有可能找到完全匹配的内容。要优化搜索,可以提供其他参数,例如搜索位置,类别,国家/地区代码和其他参数。
定位器解析地址,并使用所有参数返回一组地址候选。每个候选者都包含完整的地址,位置,属性和分数(0-100),该分数根据用于进行匹配的定位器的特征类型来表明匹配程度。无论分数如何,总是按从最准确到最不准确的顺序返回候选人;无需对返回的候选者进行排序。由于分数是相对于要素类型的,因此,例如,PointAddress比赛中较低的分数仍然比邮政比赛中较高的分数更好。默认情况下,匹配的地址,分数和位置将与地理编码结果一起返回。要返回其他可用数据字段,请在地址解析参数中设置输出字段参数。
对于场所搜索,可以提供场所名称或类别,而不是传递单行地址值作为输入。地理编码参数可以选择更接近指定位置的候选项,如果是用于在当前位置附近搜索设施,这项将会非常有用。根据用于构建定位器的数据,可以返回有关位置的信息,例如地址,电话号码和求职者的URL,以及与参考位置的距离。
在对地名进行地理编码时,可以像使用地址搜索一样使用地理编码参数来优化或限制搜索结果。
1.定位器任务
ArcGIS Runtime使用定位器任务来实现地理编码,反向地理编码或地点搜索。 使用在线或本地定位器配置定位器任务,该在线或本地定位器包含用于将地址,地名或类别与地理位置进行匹配的逻辑。
通过将URI(用于在线定位器)或路径(用于本地定位器)传递给类上的静态LocatorTask.CreateAsync()方法,可以创建LocatorTask。 以下示例使用Geocoding service创建一个新的LocatorTask。
Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey = "YOUR_API_KEY";
var locatorTask = new LocatorTask(new Uri("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"));
// Or set an APIKey on the Locator Task:
// locatorTask.ApiKey = "YOUR_API_KEY";
var results = await locatorTask.GeocodeAsync("1600 Pennsylvania Ave NW, DC");
if (results?.FirstOrDefault() is GeocodeResult firstResult)
{
Console.WriteLine($"Found {firstResult.Label} at {firstResult.DisplayLocation} with score {firstResult.Score}");
}
2.地理编码参数
对地址或地点进行地理编码时,可以选择提供地理编码操作的首选项。 以下列表描述了可以控制的一些属性:
(1)Country(国家/地区)-地理编码应限制到的国家/地区的代码。
(2)Maximum results(最大结果数)-返回的最大候选地址数的限制。
(3)Output language(输出语言)-返回结果属性所使用的语言(区域性)(支持的输出语言将根据定位器的构建方式而有所不同。)
(4)Output spatial reference(输出空间参考)-将返回地址候选者的地理位置的空间参考。
(5)Result attributes(结果属性)-要包括在结果中的字段名称的列表(可以从定位器查询可用字段。)
(6)Preferred search location(首选搜索位置)-优先考虑附近位置作为结果的地理位置。
使用GeocodeParameters对象指定地理编码首选项。
// search within Japan and return results in Japanese
var geocodeParams = new GeocodeParameters
{
CountryCode = "Japan",
OutputLanguage = new System.Globalization.CultureInfo("ja-JP")
};
geocodeParams.ResultAttributeNames.Add("City");
3.地理编码结果
根据输入地址的具体程度和完整性,可能会从地址解析操作中获得多个候选地址。 根据匹配的质量对结果进行排序,并根据相对于结果特征类型的匹配质量对结果进行评分。 应始终将第一个候选者视为最佳匹配。 通过指定补充输出字段以包括在结果中,可以获得其他候选信息。 有关可用于地址解析服务的字段的完整列表,请参阅ArcGIS REST API文档中的输出字段。 如果构建了定制定位器,则定位器将包含在定位器属性中指定的输出字段以及可能已创建的任何定制输出字段。
四.示例
搜索地址
1.创建一个新的Visual Studio项目。
2.设置API密钥
在Visual Studio的“解决方案资源管理器”中,单击App.xaml.cs。
在App类中,为OnStartup()函数添加一个替代,以在ArcGISRuntimeEnvironment上设置ApiKey属性。
// 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 System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace DisplayAMap
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(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";
}
}
}
3.添加图形叠加
图形叠加层是图形的容器。 图形叠加层通常用于显示临时信息,例如地址搜索操作的结果,并显示在所有其他层的顶部。 当保存地图时,它们不会持久存在。
(1)在Visual Studio>解决方案资源管理器中,双击MapViewModel.cs以打开文件。
该项目使用Model-View-ViewModel(MVVM)设计模式将应用程序逻辑(视图模型)与用户界面(视图)分开。 MapViewModel.cs包含应用程序的视图模型类,称为MapViewModel。
(2)向MapViewModel类添加其他必需的using语句。
Esri.ArcGISRuntime.Tasks.Geocoding包含用于地理编码(从地址中查找地理位置)的类。 Esri.ArcGISRuntime.UI命名空间包含GraphicsOverlay和Graphic类,而Esri.ArcGISRuntime.Symbology包含定义用于显示符号的类。
using System;
using System.Collections.Generic;
using System.Text;
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)在视图模型中,创建一个名为GraphicsOverlays的新属性。 这是GraphicsOverlay的集合,将存储地理编码结果图形(地址位置和标签)。
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();
}
}
(4)在SetupMap函数的末尾,添加代码以创建新的GraphicsOverlay,将其添加到集合中,并将其分配给GraphicsOverlays属性。
// 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;
}
完整版的MapViewModel代码:
// 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 System.Text;
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 = null)
{
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 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;
}
}
}
(5)在Visual Studio>解决方案资源管理器中,双击MainWindow.xaml以打开文件。
(6)使用数据绑定将MapViewModel的GraphicsOverlays属性绑定到MapView控件。
<esri:MapView x:Name="MainMapView"
Map="{Binding Map, Source={StaticResource MapViewModel}}"
GraphicsOverlays="{Binding GraphicsOverlays, Source={StaticResource MapViewModel}}"/>
4.添加用户输入的用户界面
用户将在文本框中输入地址,然后单击按钮以执行搜索。 MapViewModel类包含执行搜索的逻辑,但是控件由视图管理。
(1)在Visual Studio>解决方案资源管理器中,双击MainWindow.xaml以打开文件。
(2)在包含地址搜索控件的地图视图上方添加边框。
<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>
完整版的MainWindow.xaml
<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>
</Window>
5.创建一个对地址进行地理编码的函数
地理编码是使用定位器实现的,通常是通过引用地理编码服务(例如Geocoding服务)来创建的,或者对于离线地理编码,是通过引用包含在移动程序包中的定位器数据来创建的。 地理编码参数可用于微调结果,例如设置结果的最大数量或在结果中请求其他属性。
(1)在Visual Studio>解决方案资源管理器中,双击MapViewModel.cs以打开文件。
与MVVM设计模式保持一致,地理编码逻辑以及与用户界面无关的任何代码都存储在视图模型中。
(2)添加新功能以搜索地址并返回其地理位置。 地址字符串和空间参考(用于输出位置)将传递给该函数。 该功能被标记为异步,因为它将使用await关键字进行异步调用。
异步函数通常返回Task ,其中T是等待的调用所期望的类型。 在此功能中,对地址进行地理编码后,将返回代表地址位置的MapPoint。 如果地理编码未能找到匹配项,则返回null。
/ 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)获取GraphicsOverlay并清除其所有图形。
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();
(4)基于地理编码服务创建LocatorTask。
// 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.arcgis.com/arcgis/rest/services/World/GeocodeServer"));
(5)创建一个新的GeocodeParameters并定义其某些属性。
添加属性名称以返回到ResultAttributeNames集合。 星号(*)表示所有属性。
设置MaxResults返回的最大结果数。
使用OutputSpatialReference设置结果位置的空间参考。
// Create a locator task.
LocatorTask locatorTask = new LocatorTask(new Uri("https://geocode.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;
(6)要查找所提供地址的位置,请在locatorTask上调用GeocodeAsync,并提供地址字符串和geocodeParameters。
// 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.FirstOrDefault();
if(geocodeResult == null) { throw new Exception("No matches found."); }
6.显示结果
可以通过将图形添加到地图视图的图形叠加层来显示地理编码结果。
(1)如果找到结果,则创建两个Graphics并将它们添加到graphicsOverlay。 创建一个图形以显示地理编码结果的位置,并创建另一个图形以显示地理编码结果的标签文本(所定位的地址)。
// Geocode the address string and get the first (only) result.
IReadOnlyList<GeocodeResult> results = await locatorTask.GeocodeAsync(address, geocodeParameters);
GeocodeResult geocodeResult = results.FirstOrDefault();
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);
(2)利用结果的位置设置addressLocation以从函数中返回它。 调用函数使用此MapPoint将显示平移到结果位置。
// 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;
7.单击按钮时搜索地址
(1)在Visual Studio>解决方案资源管理器中,双击MainWindow.xaml.cs以打开文件。
用XAML定义的视图(例如MainWindow)由Visual Studio项目中的两个文件组成。 视觉元素(例如地图视图,按钮,文本框等)是通过.xaml文件中的XAML标记定义的。 该文件通常称为“页面”。 XAML元素的代码存储在关联的.xaml.cs文件中,该文件称为“隐藏代码”,因为该文件将代码存储在控件的后面。 与MVVM设计模式保持一致,用户界面事件(例如响应按钮单击的代码)在视图的代码背后进行处理。
(2)为SearchAddressButton click事件添加一个处理程序。
public MainWindow()
{
InitializeComponent();
// Create a point centered on the Santa Monica mountains in Southern California.
// Longitude=118.805 degrees West, Latitude=34.027 degrees North
var 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));
}
private async void SearchAddressButton_Click(object sender, RoutedEventArgs e)
{
}
(3)从页面资源中找到MapViewModel。
private async void SearchAddressButton_Click(object sender, RoutedEventArgs e)
{
// Get the MapViewModel from the page (defined as a static resource).
MapViewModel viewModel = FindResource("MapViewModel") as MapViewModel;
}
(4)在MapViewModel上调用SearchAddress函数,并存储返回的’MapPoint’。
private async void SearchAddressButton_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);
}
(5)如果返回了地图点,则将“ MapView”以地址结果为中心。
private async void SearchAddressButton_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);
}
}
(6)单击调试>开始调试(或按键盘上的)运行该应用程序。