关于coreLocation - 地理位置反向编码

CoreLocation中得到的定位信息都是以经度和纬度等表示的地理信息,很多时候我们需要把它反向编码成普通人能读懂的地理位置描述如:X国XX市XXX区XXX街道XX号,这就需要用到MapKit中的一个地理位置反向编码工具:MKReverseGeocoder,


用法:

1,首先要实现协议MKReverseGeocoderDelegate,因为将坐标信息发到服务器再反回来需要一定的时间,所以为了防止阻塞,发出信息后并不知到什么时候会返回信息,信息返回时会通知委托方法。这里实现这个类主要时为了实现2个方法如下

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{
    NSLog(@"MKReverseGeocoder has failed.");
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
    
    NSLog(@"当前地理信息为:%@",placemark);
}
didFailWithError这个方法是来处理返回错误信息的,didFindPlacemark则是地理信息返回了,地理信息包含在placemark里面,此对象中包含国家,城市,区块,街道等成员变量。


2,然后可以init一个反向编码器,然后发出请求了:

MKReverseGeocoder *reverseGeocoder =[[MKReverseGeocoder alloc] initWithCoordinate:coordinate];
    NSLog(@"%g",coordinate.latitude);
    NSLog(@"%g",coordinate.longitude);
    reverseGeocoder.delegate = self;
    [reverseGeocoder start];
MKReverseGeocoder除了start方法,还有cancel方法来取消请求。

你可能感兴趣的:(服务器,工具,MapKit)