GeocodeAddressAsync在某些地址上抛出NSErrorException但不是全部

时间:2017-01-24 20:38:33

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

使用Xamarin解析位置字符串时是否有任何问题?

EG。我通过地址" 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

1 个答案:

答案 0 :(得分:2)

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

CLError.h开始,代码= 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