【发布时间】:2023-04-03 03:45:01
【问题描述】:
我有一个简单的问题。
MKReverseCoder 已被弃用,并且自 iOS 5 起无法使用。我们必须使用CLGeocoder。但是,iOS4下的人很多。新应用如何与 iOS4 和 iOS5 一起使用进行地理编码?
谢谢!
【问题讨论】:
标签: iphone ios5 backwards-compatibility reverse-geocoding
我有一个简单的问题。
MKReverseCoder 已被弃用,并且自 iOS 5 起无法使用。我们必须使用CLGeocoder。但是,iOS4下的人很多。新应用如何与 iOS4 和 iOS5 一起使用进行地理编码?
谢谢!
【问题讨论】:
标签: iphone ios5 backwards-compatibility reverse-geocoding
- (IBAction)geoCodeLocation:(id)sender{
[self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler:
^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"placemark %@",placemark);
//String to hold address
NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
NSLog(@"addressDictionary %@", placemark.addressDictionary);
NSLog(@"placemark %@",placemark.region);
NSLog(@"placemark %@",placemark.country); // Give Country Name
NSLog(@"placemark %@",placemark.locality); // Extract the city name
NSLog(@"location %@",placemark.name);
NSLog(@"location %@",placemark.ocean);
NSLog(@"location %@",placemark.postalCode);
NSLog(@"location %@",placemark.subLocality);
NSLog(@"location %@",placemark.location);
//Print the location to console
NSLog(@"I am currently at %@",locatedAt);
//Set the label text to current location
[locationLabel setText:locatedAt];
}];
【讨论】:
如果有人试图从 MKReverseGeocoder 迁移到 CLGeocoder,那么我写了一篇博文,可能会有所帮助 http://jonathanfield.me/jons-blog/clgeocoder-example.html
基本上一个例子是,在您创建 locationManager 和 CLGeocoder 对象后,只需将此代码添加到您的 viewDidLoad(),然后制作一些标签或文本区域来显示数据。
[super viewDidLoad]; locationManager.delegate = self; [locationManager startUpdatingLocation];
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[self.CLGeocoder reverseGeocodeLocation: locationManager.location completionHandler:
^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
isoCountryCode.text = placemark.ISOcountryCode;
country.text = placemark.country;
postalCode.text= placemark.postalCode;
adminArea.text=placemark.administrativeArea;
subAdminArea.text=placemark.subAdministrativeArea;
locality.text=placemark.locality;
subLocality.text=placemark.subLocality;
thoroughfare.text=placemark.thoroughfare;
subThoroughfare.text=placemark.subThoroughfare;
//region.text=placemark.region;
}];
【讨论】:
MKReverseGeocoder 仍然适用于 iOS5。它只是被弃用了,这意味着它将在稍后的某个时间点被删除(比如在 iOS6 之类的发布时)。所以你现在可以继续使用它,没有任何问题
【讨论】: