- 關(guān)于
地理編碼是通過(guò)地名而獲取經(jīng)緯度伪煤;
地理反編碼是通過(guò)經(jīng)緯度而獲取地名舍肠;
代碼部分:
@property (nonatomic,strong) CLGeocoder *geoCoder;
通過(guò)懶加載創(chuàng)建編碼對(duì)象:
- (CLGeocoder *)geoCoder{
if (!_geoCoder) {
_geoCoder = [[CLGeocoder alloc]init];
}
return _geoCoder;
}
- 實(shí)現(xiàn)地理編碼的方法:
- (void)geocoder:(NSString *)name{
// 地理信息編碼/反編碼對(duì)象(創(chuàng)建編碼對(duì)象)
_geoCoder = [[CLGeocoder alloc] init];
// 地理信息正向編碼(地名轉(zhuǎn)換成經(jīng)緯度)
[_geoCoder geocodeAddressString:name completionHandler:^(NSArray *placemarks, NSError *error) {
// 對(duì)編碼結(jié)果進(jìn)行迭代
for (CLPlacemark *pMark in placemarks) {
NSLog(@"location:%@", pMark.location);
// 將地址字典轉(zhuǎn)換成二進(jìn)制數(shù)據(jù)
NSData *data = [NSJSONSerialization dataWithJSONObject:pMark.addressDictionary options:NSJSONWritingPrettyPrinted error:nil];
// 將二進(jìn)制數(shù)據(jù)轉(zhuǎn)成字符串
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str%@", str);
}
}];
}
- 實(shí)現(xiàn)地理反編碼的方法:
- (void)ungeocoder{
_geoCoder = [[CLGeocoder alloc] init];
CLLocation *loc = [[CLLocation alloc] initWithLatitude:22.68359500 longitude:114.22838700];
// 反編碼地理信息(將經(jīng)緯度轉(zhuǎn)換成地名)
[_geoCoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *pMark = [placemarks firstObject];
MKPlacemark *mkMark = [[MKPlacemark alloc] initWithPlacemark:pMark];
// 開(kāi)啟地圖應(yīng)用的加載項(xiàng)
NSDictionary *options = @{ MKLaunchOptionsMapTypeKey: @(MKMapTypeStandard),
MKLaunchOptionsShowsTrafficKey: @(YES)
};
// 創(chuàng)建一個(gè)地圖應(yīng)用項(xiàng)
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:mkMark];
// 開(kāi)啟一個(gè)地圖應(yīng)用
[mapItem openInMapsWithLaunchOptions:options];
///添加地圖應(yīng)用項(xiàng)時(shí)打開(kāi)它就會(huì)自動(dòng)跳轉(zhuǎn)到蘋(píng)果地圖籍凝,則可以應(yīng)用蘋(píng)果地圖進(jìn)行各種巡李,比如路線掘猿,導(dǎo)航等等(由此特點(diǎn)考慮做旅游應(yīng)用可以否?)
}];
}