CLGeocoder和向后兼容性

6
我有一个简单的问题。
MKReverseCoder已被弃用,自iOS 5以来不再可用。我们必须使用CLGeocoder。但是,有很多人仍在使用iOS4。新的应用程序如何在iOS4和iOS5上进行地理编码?
谢谢!
3个回答

8
如果有人想从MKReverseGeocoder转向CLGeocoder,那么我写了一篇博客文章可能会有所帮助。基本上,一个示例是,在您创建了locationManager和CLGeocoder对象之后,只需将此代码添加到您的viewDidLoad()中,然后创建一些标签或文本区域来显示数据。您可以参考这个链接:http://jonathanfield.me/jons-blog/clgeocoder-example.html。请注意不要更改HTML标记。
[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;

}];

如何初始化CLGeoCoder对象? - Satyam
这并没有提供完整的邮政编码!它只提供类似于“HP2 7”的内容,最后两个字符丢失了。你知道可能是什么原因吗?如果我查询谷歌API - http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true,它会返回完整的邮政编码! - Paresh Masani

4

MKReverseGeocoder仍可在iOS5上使用,只是已弃用,这意味着它将在稍后的某个时间被移除(比如在类似iOS6的发布时)。因此,您现在可以继续使用它而不会遇到任何问题。


我收到了这样的消息:Error Domain=NSURLErrorDomain Code=-1011 "The operation couldn’t be completed. (NSURLErrorDomain error -1011.)"。而且之前它是可以工作的... - Mathieu Mahé
这确实很奇怪。我认为这是一个身份验证错误,但找到其源头将是一个问题。看起来MKReverseGeocoder仍然在我的应用程序上工作(构建于iOS4.3,但在iOS5设备上运行),所以我觉得错误来自其他地方(或可能是Google服务器的临时问题)。 - justin
好的。现在它可以工作了。那只是暂时的,与我的iOS5升级非常不巧合...谢谢你的帮助! - Mathieu Mahé
很高兴听到它现在可以工作了,我的荣幸。但是,不要太担心已弃用的方法。在实际删除之前,它们将继续工作一段时间,为您和用户提供足够的时间来切换到新方法和新操作系统。 - justin

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

     }];

更多信息请参见CLPlacemark的属性


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接