代码改变世界

MapKit库有关反向地理编码(如何根据经纬度查找地理信息)

2011-12-26 14:43  张智清  阅读(581)  评论(0编辑  收藏  举报

与地图打交道时,有时需要查找经纬度获取地址信息,MapKit提供了这样一种工具——反向地理编码MKReverseGeocoder

MKReverseGeocoder *reverseGeocoder = [[[MKReverseGeocoder alloc] initWithCoordinate:self.mapView.userLocation.location.coordinate] autorelease];
NSLog(@"%g",self.mapView.userLocation.location.coordinate.latitude);
NSLog(@"%g",self.mapView.userLocation.location.coordinate.longitude);
reverseGeocoder.delegate = self;
[reverseGeocoder start];

然后实现下面两个代理方法即可获得你想要的地理信息

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {
NSLog(@"MKReverseGeocoder has failed.");
}


- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
NSLog(@"当前地理信息为:%@",placemark.locality);
}