CLGeocoder和向后兼容性

时间:2011-10-18 14:43:38

标签: iphone ios5 backwards-compatibility reverse-geocoding

我有一个简单的问题。

MKReverseCoder已弃用,自<​​strong> iOS 5 以来无法使用。我们必须使用CLGeocoder。但是, iOS4 下有很多人。新应用如何与 iOS4和iOS5 配合使用进行地理编码?

谢谢!

3 个答案:

答案 0 :(得分:8)

如果有人试图从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;

}];

答案 1 :(得分:4)

MKReverseGeocoder仍适用于iOS5。它刚刚被弃用,这意味着它将在稍后被删除(就像在iOS6这样的版本发布时)。因此,您现在可以继续使用它而不会出现任何问题

答案 2 :(得分:1)

- (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];

     }];

有关详情,请参阅property of CLPlacemark