Share via


CivicAddressResolver.ResolveAddressAsync(GeoCoordinate) 方法

定义

从基于经度和纬度的位置异步解析地址。 解析地址时,调用不会阻止执行调用线程。

public:
 virtual void ResolveAddressAsync(System::Device::Location::GeoCoordinate ^ coordinate);
public void ResolveAddressAsync (System.Device.Location.GeoCoordinate coordinate);
abstract member ResolveAddressAsync : System.Device.Location.GeoCoordinate -> unit
override this.ResolveAddressAsync : System.Device.Location.GeoCoordinate -> unit
Public Sub ResolveAddressAsync (coordinate As GeoCoordinate)

参数

coordinate
GeoCoordinate

GeoCoordinate,它含有要解析为市镇地址的位置。

实现

例外

coordinatenull

coordinate 不包含有效纬度和经度。

示例

以下程序演示如何调用 ResolveAddressAsync 以异步解析市政地址。

using System;
using System.Device.Location;
namespace ResolveAddressSync
{
    class AsyncProgram
    {
        public static void Main(string[] args)
        {
            ResolveAddressAsync();
        }
        static void ResolveAddressAsync()
        {
            GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
            bool started = false;
            watcher.MovementThreshold = 1.0; // set to one meter
            started = watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));

            if (started)
            {
                CivicAddressResolver resolver = new CivicAddressResolver();

                resolver.ResolveAddressCompleted += new EventHandler<ResolveAddressCompletedEventArgs>(resolver_ResolveAddressCompleted);

                if (watcher.Position.Location.IsUnknown == false)
                {
                    resolver.ResolveAddressAsync(watcher.Position.Location);
                }
            }
        }

        static void resolver_ResolveAddressCompleted(object sender, ResolveAddressCompletedEventArgs e)
        {
            if (!e.Address.IsUnknown)
            {
                Console.WriteLine("Country: {0}, Zip: {1}",
                           e.Address.CountryRegion,
                           e.Address.PostalCode);
            }
            else
            {
                Console.WriteLine("Unknown address.");
            }
        }

    }
}
Imports System.Device.Location

Module ResolveCivicAddressAsync

    Public Sub ResolveCivicAddressAsync()
        Dim watcher As GeoCoordinateWatcher
        watcher = New System.Device.Location.GeoCoordinateWatcher(GeoPositionAccuracy.High)
        Dim started As Boolean = False
        watcher.MovementThreshold = 1.0     'set to one meter
        started = watcher.TryStart(False, TimeSpan.FromMilliseconds(1000))
        If started Then
            Dim resolver As CivicAddressResolver = New CivicAddressResolver()
            AddHandler resolver.ResolveAddressCompleted, AddressOf resolver_ResolveAddressCompleted
            If Not watcher.Position.Location.IsUnknown Then
                resolver.ResolveAddressAsync(watcher.Position.Location)
            End If
        End If

        watcher.Start()

    End Sub

    Sub resolver_ResolveAddressCompleted(ByVal sender As Object, ByVal e As ResolveAddressCompletedEventArgs)
        If Not e.Address.IsUnknown Then
            Console.WriteLine("Country: {0}, Zip: {1}",
                           e.Address.CountryRegion,
                           e.Address.PostalCode)
        Else
            Console.WriteLine("Unknown address.")
        End If
    End Sub


    Public Sub Main()

        ResolveCivicAddressAsync()
        Console.WriteLine("Enter any key to quit.")
        Console.ReadLine()
    End Sub

End Module

注解

此方法存储在任务中,它返回该方法的同步对应项可能引发的所有非使用异常。 如果异常存储在返回的任务中,则在等待任务时将引发该异常。 使用异常(如 ArgumentException)仍会同步引发。 有关存储的异常,请参阅 引发的 ResolveAddress(GeoCoordinate)异常。

适用于