编码反编码编码与反编码用到的类都是CLGeocoder - 编码:将地址转化为经纬度
反编码:将经纬度转化为地址 之后我们会接触到一个CLPlacemark类,他是定位框架中地标类,封装了详细的地理信息
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
示例:
#import "CLGeocoderViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface CLGeocoderViewController ()
{
CLGeocoder *_geocoder;
}
@end
@implementation CLGeocoderViewController
- (void)viewDidLoad {
[super viewDidLoad];
_geocoder=[[CLGeocoder alloc]init];
[self getCoordinateByAddress:@"北京"];
[self getAddressByLatitude:39.54 longitude:116.28];
}
#pragma mark 根据地名确定地理坐标
-(void)getCoordinateByAddress:(NSString *)address{
[_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark=[placemarks firstObject];
CLLocation *location=placemark.location;
CLRegion *region=placemark.region;
NSDictionary *addressDic= placemark.addressDictionary;
NSLog(@"位置:%@,区域:%@,详细信息:%@",location,region,addressDic);
}];
}
#pragma mark 根据坐标取得地名
-(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(@"详细信息:%@",placemark.addressDictionary);
}];
}
@end
总结我们目前在地图上接触到的几个类: CLLocationManager:定位管理器,用来设置管理定位,设置定位的精度、定位频率、后台运行等。 CLGeocoder:主要用来编码与反编码。 CLLocation:用于表示位置信息,包含地理坐标、海拔等信息,包含在CoreLoaction框架中。 CLPlacemark:定位框架中地标类,封装了详细的地理信息。 CLLocationCoordinate2D:他是一个结构体,用来表示经纬度。
|