【问题标题】:GeocodeAddressAsync is throwing a NSErrorException on some addresses but not allGeocodeAddressAsync 在某些地址上抛出 NSErrorException 但不是全部
【发布时间】:2017-06-09 20:44:54
【问题描述】:

有人在使用 Xamarin 解析位置字符串时遇到问题吗?

例如。我将地址“Buffalo, NY 14267”传递给 MonoTouch.CoreLocation.CLGeocoder.GeocodeAddressAsync(),然后抛出一个无用的异常。

但是,如果我使用“Addison, TX 75001”之类的地址,则不会出现错误。

代码很简单:

CLPlacemark[] placemarks = await _geoCoder.GeocodeAddressAsync(new CLLocation((double)location.Latitude, (double)location.Longitude));

编辑:我使用的是 GeocodeAddressAsync 而不是 ReverseGeocodeLocationAsync

【问题讨论】:

  • 什么是“无用”异常?你看过可能的内部异常吗?您的示例代码是错误的。您粘贴了相反的。

标签: ios xamarin xamarin.ios geolocation core-location


【解决方案1】:

如果您从ReverseGeocodeLocationAsync 得到NSErrorException GeocodeAddressAsync,那么我会假设您提供的经度或/和纬度或地址不正确并获得一个域 = kCLErrorDomaincode = 8,因此 Apple 没有关于您的请求的数据。

来自CLError.h,code = 8 是:

kCLErrorGeocodeFoundNoResult,  // A geocode request yielded no result

NSErrorException 上尝试/捕获并确定错误的域/代码:

示例:

try
{
    var _geoCoder = new CLGeocoder();
    var placemarks = await _geoCoder.ReverseGeocodeLocationAsync(new CLLocation(42.885441, -78.878471));
    foreach (var placemark in placemarks)
    {
        Console.WriteLine(placemark.ToString());
    }
    placemarks = await _geoCoder.ReverseGeocodeLocationAsync(new CLLocation(232.965158, -96.876839));
    foreach (var placemark in placemarks)
    {
        Console.WriteLine(placemark.ToString());
    }
}
catch (Exception ex) when (ex is Foundation.NSErrorException)
{
    var nsErrorException = ex as Foundation.NSErrorException;
    Console.WriteLine($"{nsErrorException.Domain}:{nsErrorException.Code}");
}

输出:

50 Delaware Ave, 50 Delaware Ave, Buffalo, NY  14202, United States @ <+42.88550930,-78.87880640> +/- 100.00m, region CLCircularRegion (identifier:'<+42.88550930,-78.87880640> radius 70.84', center:<+42.88550930,-78.87880640>, radius:70.84m)
kCLErrorDomain:8

【讨论】:

  • 我在原始问题中打错了:我使用的是 GeocodeAddressAsync 而不是 ReverseGeocodeLocationAsync
  • @msmt 无论哪种方式,尝试/捕获与我的示例相同的方式并检查NSErrorException 中的域/代码是什么,您对GeocodeAddressAsync 存在的潜在问题的回答将在那里。
  • @msmt 我假设你会得到相同的结果(kCLErrorDomain:8,因为邮政编码 14267 在邮局注册的企业、单户和多户地址为零因此 Apple 的数据也为零。请尝试 Buffalo, NY 14202.... 我用CLError.h 的 code=8 Objective-C 枚举描述更新了我的答案
  • 你是对的。我收到错误代码 8。这是我第一次做任何真正的地理定位工作,所以你有什么建议可以在地理定位失败时回退吗?任何最佳做法(例如,如果 14267 不起作用,则推荐另一个靠近的位置...)
  • @msmt 您绝对必须为您从 Apple、Google 和其他 3rd 方服务获得零结果的时候进行防御性编码,正如您亲眼看到的那样,这将会发生,问题变成了用户体验,只有您可以回答在这种情况下您向用户展示的内容/方式。你知道地址是City, ST, Zip,你能把邮编去掉吗?如果您可以删除邮政编码和城市等...在某些州,邮政编码区域可能很大(!),您可以添加街道、十字路口、地标等...如果您是从该地址获取地址,可以用户在失败时添加更多信息?
猜你喜欢
  • 1970-01-01
  • 2021-08-04
  • 2021-12-26
  • 1970-01-01
  • 2017-08-18
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多