地理編碼
除了提供位置跟蹤功能之外,在定位服務(wù)中還包含CLGeocoder類用于處理地理編碼和逆地理編碼(又叫反地理編碼)功能.
地理編碼:根據(jù)給定的位置(通常是地名)確定地理坐標(biāo)(經(jīng)糠赦、緯度).
逆地理編碼:可以根據(jù)地理坐標(biāo)(經(jīng)、緯度)確定位置信息(街道熬甫、門牌等).
CLGeocoder最主要的兩個(gè)方法就是- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;和- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;,分別用于地理編碼和逆地理編碼必盖。下面簡單演示一下:
//
// KCMainViewController.m
// CoreLocation
//
// Created by Kenshin Cui on 14-03-27.
// Copyright (c) 2014年 Kenshin Cui. All rights reserved.
//
#import "KCMainViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface KCMainViewController ()<CLLocationManagerDelegate>{
CLGeocoder *_geocoder;
}
@end
@implementation KCMainViewController
- (void)viewDidLoad {
[super viewDidLoad];
_geocoder=[[CLGeocoder alloc]init];
[self getCoordinateByAddress:@"北京"];
[self getAddressByLatitude:39.54 longitude:116.28];
}
#pragma mark 根據(jù)地名確定地理坐標(biāo)
-(void)getCoordinateByAddress:(NSString *)address{
//地理編碼
[_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
//取得第一個(gè)地標(biāo)澎蛛,地標(biāo)中存儲(chǔ)了詳細(xì)的地址信息,注意:一個(gè)地名可能搜索出多個(gè)地址
CLPlacemark *placemark=[placemarks firstObject];
CLLocation *location=placemark.location;//位置
CLRegion *region=placemark.region;//區(qū)域
NSDictionary *addressDic= placemark.addressDictionary;//詳細(xì)地址信息字典,包含以下部分信息
// NSString *name=placemark.name;//地名
// NSString *thoroughfare=placemark.thoroughfare;//街道
// NSString *subThoroughfare=placemark.subThoroughfare; //街道相關(guān)信息羹与,例如門牌等
// NSString *locality=placemark.locality; // 城市
// NSString *subLocality=placemark.subLocality; // 城市相關(guān)信息德谅,例如標(biāo)志性建筑
// NSString *administrativeArea=placemark.administrativeArea; // 州
// NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政區(qū)域信息
// NSString *postalCode=placemark.postalCode; //郵編
// NSString *ISOcountryCode=placemark.ISOcountryCode; //國家編碼
// NSString *country=placemark.country; //國家
// NSString *inlandWater=placemark.inlandWater; //水源爹橱、湖泊
// NSString *ocean=placemark.ocean; // 海洋
// NSArray *areasOfInterest=placemark.areasOfInterest; //關(guān)聯(lián)的或利益相關(guān)的地標(biāo)
NSLog(@"位置:%@,區(qū)域:%@,詳細(xì)信息:%@",location,region,addressDic);
}];
}
#pragma mark 根據(jù)坐標(biāo)取得地名
-(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{
//反地理編碼
CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
[_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark=[placemarks firstObject];
NSLog(@"詳細(xì)信息:%@",placemark.addressDictionary);
}];
}
@end