【问题标题】:How can I call MapLocationFinder.FindLocationsAsync() to get the coordinates for an address without already knowing the latitude and longitude?如何在不知道经纬度的情况下调用 MapLocationFinder.FindLocationsAsync() 来获取地址的坐标?
【发布时间】:2020-12-12 16:34:24
【问题描述】:

我正在尝试修改代码 here 以获取传入地址的纬度和经度。

但是,传递给 MapLocationFinder.FindLocationsAsync() 的参数之一是已经设置了纬度和经度的 Geopoint,就像上面引用的代码中这样:

// The nearby location to use as a query hint.
   BasicGeoposition queryHint = new BasicGeoposition();
   queryHint.Latitude = 47.643;
   queryHint.Longitude = -122.131;
   Geopoint hintPoint = new Geopoint(queryHint);

   // Geocode the specified address, using the specified reference point
   // as a query hint. Return no more than 3 results.
   MapLocationFinderResult result =
         await MapLocationFinder.FindLocationsAsync(
                           addressToGeocode,
                           hintPoint,
                           3);

最终的结果就是设置了经纬度:

   if (result.Status == MapLocationFinderStatus.Success)
   {
      tbOutputText.Text = "result = (" +
            result.Locations[0].Point.Position.Latitude.ToString() + "," +
            result.Locations[0].Point.Position.Longitude.ToString() + ")";
   }

这对我来说没有意义 - 该方法的全部目的是设置纬度和经度,换句话说,在调用 MapLocationFinder.FindLocationsAsync() 时它们是未知的。

这是我的代码,我对上面和给定链接的内容进行了改编:

public sealed partial class MainPage : Page
{
    . . .
    // Global, set by call to SetLatAndLongForAddress()
    double latitude = 0.0;
    double longitude = 0.0;
    . . .

private async void SetLatAndLongForAddress(string fullAddress)
{
    try
    {
        string addressToGeocode = fullAddress;

        MapLocationFinderResult mapLocFinderResult =
            await MapLocationFinder.FindLocationsAsync(
                addressToGeocode,
                null, 
                1); // changed 3 to 1 - only want one returned val

        if (mapLocFinderResult.Status == MapLocationFinderStatus.Success)
        {
            latitude = mapLocFinderResult.Locations[0].Point.Position.Latitude;
            longitude = mapLocFinderResult.Locations[0].Point.Position.Longitude;
        }
    }
    . . .
}

这是执行此操作的方法吗 - 将 null 而不是 Geopoint 作为第二个参数传递给 MapLocationFinder.FindLocationsAsync()?这会起作用吗,或者我对此有什么误解?

【问题讨论】:

    标签: c# uwp bing-maps geopoints uwp-maps


    【解决方案1】:

    是的,你的方式没问题。

    FindLocationsAsync(String, Geopoint, UInt32) 方法会将指定的地址转换为地理位置的集合(地理编码)。指定地址由你的fullAddress提供,第二个参数Geopoint只是一个提示,不是依据。第二个参数可以为null,方法可以工作。

    当你想得到一个地址的坐标时,你可以给出一个与你的目标地址接近的已知坐标,这可能有助于搜索你的目标地址的坐标。

    【讨论】:

      【解决方案2】:

      您传递给 FindLocationsAsync 的 Geopoint 是一个“提示”,可帮助地理编码器给出正确的结果。例如,如果您搜索“巴黎”并且位于德克萨斯州的农村,您可能希望获得最佳结果,德克萨斯州的巴黎。在其他任何地方,您可能想要法国巴黎。 传递 null 将尝试在没有提示的情况下进行地理编码,因此您可能无法获得最佳结果。 人们经常传递用户当前位置(使用 geolocaton API https://docs.microsoft.com/en-us/windows/uwp/maps-and-location/get-location)或地图正在查看的当前位置(如果您有一个可见的地图控件(使用地图控件中心属性))。

      【讨论】:

      • 谢谢;当有人在自己的区域制作地图时,我可以看到这很有帮助。
      猜你喜欢
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 2010-09-14
      • 2015-06-08
      • 1970-01-01
      • 2013-02-23
      相关资源
      最近更新 更多