考虑到位置的水平精度,是否有一个很好的解决方案来反转地理编码?
例如,这是位置的结果(无论水平精度如何):
{
City = "San Francisco";
Country = "United States";
CountryCode = US;
FormattedAddressLines = (
"246 Powell St",
"San Francisco, CA 94102-2206",
"United States"
);
Name = "246 Powell St";
PostCodeExtension = 2206;
State = CA;
Street = "246 Powell St";
SubAdministrativeArea = "San Francisco";
SubLocality = "Union Square";
SubThoroughfare = 246;
Thoroughfare = "Powell St";
ZIP = 94102;
}
我想得到考虑准确性的结果。 E.g:
我想我可以通过请求反向地理编码来实现这一点,这些坐标大致位于所提供的水平精度的边缘,然后与结果相交。但是有更干净的方式吗?
答案 0 :(得分:0)
您的意思是CLGeocoder
吗?
我认为请求多次反向地理编码可能会造成更大的危害
来自Apple的CLGeocoder
文档(可以找到here):
Applications should be conscious of how they use geocoding.
Geocoding requests are rate-limited for each app,
so making too many requests in a short period of time
may cause some of the requests to fail.
所以我建议不要这样做。
可能是更好的方法,但只是在我的头脑中,你可能会使用某种使用if-else
属性的horizontalAccuracy
方法。
自从我使用MapKit已经有一段时间了,所以我的代码可能不是100%准确但我现在无法测试其功能(它应该虽然编译),但它会让你知道如何做到这一点。
例如:
// here geocoder is your CLGeocoder object
// and location is the CLLocation you try to reverse geocode
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if(error) {
NSLog(@"Error occurred: %@", [error localizedDescription]);
} else { // No error has occurred
if([placemarks count]) { // Just another step of precaution
CLPlacemark *placemark = placemarks[0]; // assume the first object is our correct place mark
if(location.horizontalAccuracy <= 10) { // equal or less than 10 meters
NSLog(@"Result: %@ %@", placemark.subThoroughfare, placemark.thoroughfare); // 246 Powell St
} else if (location.horizontalAccuracy <= 100) { // equal or less than 100 meters
NSLog(@"Result: %@", placemark.subLocality); // Union Square
} else if (location.horizontalAccuracy <= 100000) { // equal or less than 100km
NSLog(@"Result: %@", placemark.subAdministrativeArea); // San Francisco
}
}
}
}];
当我能够自己测试其功能时(可能需要几个小时,或几天),如果需要进行更改,我会编辑。
如果您遇到问题或者您有更多问题,请告诉我。
祝你好运。