所以我一直在研究这个问题,但我似乎无法在任何地方找到解决方案。我正在尝试使用 CLGeocoder
在 iOS 应用程序中查找用户在 map 上长按的最近的城市。我有两个主要问题:
CLGeocoder
经常返回附近的城市,而不是更明显的答案纽约。我不知道如何设置搜索的“模糊性”来克服这个问题。 City
返回的地标对象中的字段为空,最常见于沙漠或海洋等偏远地区。在这种情况下,理想情况下,我希望找到实际上具有 City
的最近对象。已定义,但无法弄清楚如何执行此操作。 最佳答案
使用 CLGeocoder
获取您的位置的所有详细信息
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[currentLocation stopUpdatingLocation];
CLLocation *myLocation = [[CLLocation alloc] initWithLatitude:latitudeValue longitude:longitudeValue];
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:myLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if(!error)
{
for (CLPlacemark * placemark in placemarks)
{
NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode);
NSLog(@"placemark.country %@",placemark.country);
NSLog(@"placemark.postalCode %@",placemark.postalCode);
NSLog(@"placemark.administrativeArea %@",placemark.administrativeArea);
NSLog(@"placemark.locality %@",placemark.locality);
NSLog(@"placemark.subLocality %@",placemark.subLocality);
NSLog(@"placemark.subThoroughfare %@",placemark.subThoroughfare);
}
}
else
{
NSLog(@"failed getting city: %@", [error description]);
}
}];
}
关于iphone - 使用 CLGeocoder 查找最近的城市,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14834429/