//声明一个完成信息转换后的block函数
typedef void (^CLGeocodeCompletionHandler)(NSArray *placemarks, NSError *error);
@interface CLGeocoder : NSObject
@property (nonatomic, readonly, getter=isGeocoding) BOOL geocoding; //是否正在编码
//将地图上的位置信息转化为地理信息
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
//将地理信息转换为地图上的位置信息
- (void)geocodeAddressDictionary:(NSDictionary *)addressDictionary completionHandler:(CLGeocodeCompletionHandler)completionHandler;
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
- (void)geocodeAddressString:(NSString *)addressString inRegion:(CLRegion *)region completionHandler:(CLGeocodeCompletionHandler)completionHandler;
//取消位置信息编码
- (void)cancelGeocode;
@end
====================================================
//地图上的位置标记类(封装存放着一个位置转换到地图上的地理信息,包括国家、城市、大洋、街道等)
@interface MKMapItem : NSObject
@property (nonatomic, readonly) MKPlacemark *placemark;
@property (nonatomic, readonly) BOOL isCurrentLocation;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *phoneNumber;
@property (nonatomic, strong) NSURL *url;
//创建地图位置标记
+ (MKMapItem *)mapItemForCurrentLocation;
- (instancetype)initWithPlacemark:(MKPlacemark *)placemark;
//调用系统地图显示当前设置的位置
- (BOOL)openInMapsWithLaunchOptions:(NSDictionary *)launchOptions;
+ (BOOL)openMapsWithItems:(NSArray *)mapItems launchOptions:(NSDictionary *)launchOptions;
封装的地理位置标记(一些地理信息)
@interface CLPlacemark : NSObject <NSCopying, NSSecureCoding>
- (instancetype)initWithPlacemark:(CLPlacemark *)placemark;
@property (nonatomic, readonly, copy) CLLocation *location;
@property (nonatomic, readonly, copy) CLRegion *region;
@property (nonatomic, readonly, copy) NSDictionary *addressDictionary;
@property (nonatomic, readonly, copy) NSString *name; // eg. Apple Inc.
@property (nonatomic, readonly, copy) NSString *thoroughfare; // street address, eg. 1 Infinite Loop
@property (nonatomic, readonly, copy) NSString *subThoroughfare; // eg. 1
@property (nonatomic, readonly, copy) NSString *locality; // city, eg. Cupertino
@property (nonatomic, readonly, copy) NSString *subLocality; // neighborhood, common name, eg.
@property (nonatomic, readonly, copy) NSString *administrativeArea; // state, eg. CA
@property (nonatomic, readonly, copy) NSString *subAdministrativeArea; // county, eg. Santa Clara
@property (nonatomic, readonly, copy) NSString *postalCode; // zip code, eg. 95014
@property (nonatomic, readonly, copy) NSString *ISOcountryCode; // eg. US
@property (nonatomic, readonly, copy) NSString *country; // eg. United States
@property (nonatomic, readonly, copy) NSString *inlandWater; // eg. Lake Tahoe
@property (nonatomic, readonly, copy) NSString *ocean; // eg. Pacific Ocean
@property (nonatomic, readonly, copy) NSArray *areasOfInterest; // eg. Golden Gate Park
@end
// 定位管理器
_mgr = [[CLLocationManager alloc] init];
// 获取授权
[_mgr requestAlwaysAuthorization];
// 设置代理
_mgr.delegate = self;
// 设置精度
_mgr.desiredAccuracy = kCLLocationAccuracyBest;
// 开始获取用户的位置
[_mgr startUpdatingLocation];
定位管理器的代理方法:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations•
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
通过地址获得经纬度 :
CLGeocoder:地理信息编解码
_geocoder = [[CLGeocoder alloc] init];
//将地理信息转换为地图信息
[_geocoder geocodeAddressString:@“东三旗" completionHandler:^(NSArray *placemarks, NSError *error) {
//没有找到符合要求的地址
if (placemarks.count == 0) return;
// 任意取出一个位置
CLPlacemark *firstPlacemark = placemarks[0];
// 添加大头针
MyAnnotation *anno = [[MyAnnotation alloc] init];
anno.title = firstPlacemark.name; // 名称
anno.subtitle = firstPlacemark.country; // 国家
anno.coordinate = firstPlacemark.location.coordinate; // 坐标
[_mapView addAnnotation:anno]; //将大头针添加到地图中
}];
大头针->获取地理地址
//转换地图信息(将地图信息转换为具体的地理信息)
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
//任意取出一个位置
CLPlacemark *placeMark = placemarks[0];
NSLog(@"%@,%@",placeMark.country,placeMark.locality);
}];
具体实例如下:
前期准备:
1、导入CoreLocation和Mapkit框架
2、在info.plist文件中添加用户定位授权
3、主要的文件截图为:
下面就是代码的操作了:
1、声明相关的属性并实现定位协议
#import "ViewController.h" #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> @interface ViewController ()<CLLocationManagerDelegate> //实现定位管理者协议 @property (strong,nonatomic)CLLocationManager *locationManager; //声明定位管理者 @end
2、创建定位管理者,设置代理,开始定位
- (void)viewDidLoad { [super viewDidLoad]; //创建位置管理者 self.locationManager = [[CLLocationManager alloc]init]; //获取用户授权 [self.locationManager requestAlwaysAuthorization]; //定位授权状态(始终定位、当使用时定位) CLAuthorizationStatus status = [CLLocationManager authorizationStatus]; if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) { NSLog(@"授权通过"); } else { NSLog(@"授权不通过"); } //设置定位管理器的代理 self.locationManager.delegate = self; //设置精度 self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; //开始定位 [self.locationManager startUpdatingLocation]; }
3、实现定位协议的代理方法,从地图上获取任意一个位置的地理信息,以及定位失败时的处理
#pragma mark -实现定位管理者的代理方法 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { NSLog(@"%@",locations); //取出一个位置信息 CLLocation *location = [locations lastObject]; //定义地理信息编解码 CLGeocoder *geocoder = [[CLGeocoder alloc]init]; //转换地图信息(将地图信息转换为具体的地理信息) [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { //取出位置标记 CLPlacemark *placeMark = placemarks[0]; NSLog(@"%@,%@",placeMark.country,placeMark.locality); }]; } //定位失败 -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"%@",[error localizedDescription]); }
4、实现触摸开始事件,将两个地理位置添加到地图中并设置大头针,调用系统地图,最终显示两个地点的线路
//触摸开始处理 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //定义地理信息编解码 CLGeocoder *geocoder = [[CLGeocoder alloc]init]; //转换地理信息(将地理信息转换为具体的地图信息) [geocoder geocodeAddressString:@"东三旗" completionHandler:^(NSArray *placemarks, NSError *error) { if ([placemarks count] == 0) { NSLog(@"没找到相应的信息"); } else { //取出位置标记 CLPlacemark *placeMark = placemarks[0]; NSLog(@"%@%@",placeMark.country,placeMark.locality); //位置标记被封装在地图上的具体信息(包括国家、城市等等) MKMapItem *mapitem = [[MKMapItem alloc]initWithPlacemark:(MKPlacemark*)placeMark]; //转换地理信息(将地理信息转换为具体的地图信息) [geocoder geocodeAddressString:@"八达岭长城" completionHandler:^(NSArray *placemarks, NSError *error) { //当前位置在地图上的具体信息 MKMapItem *mapitem2 = [[MKMapItem alloc]initWithPlacemark:((MKPlacemark*)placemarks[0])]; //调用系统地图显示当前设置的位置 [MKMapItem openMapsWithItems:@[mapitem,mapitem2] launchOptions:nil]; }]; } }]; }
演示结果:
一开始时的用户授权:
点击Allow后输出:(此位置时间一直在更新,我只给出部分结果)
切换位置,选择此处的Hong Kong,China位置后:
输出结果:
2015-10-20 13:36:59.989 01-CLLocation[1888:127652] 授权通过 2015-10-20 13:37:00.026 01-CLLocation[1888:127652] ( "<+22.28468100,+114.15817700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 15/10/20 \U4e2d\U56fd\U6807\U51c6\U65f6\U95f4\U4e0b\U53481:33:50" ) 2015-10-20 13:37:00.034 01-CLLocation[1888:127652] ( "<+22.28468100,+114.15817700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 15/10/20 \U4e2d\U56fd\U6807\U51c6\U65f6\U95f4\U4e0b\U53481:37:00" ) 2015-10-20 13:37:00.846 01-CLLocation[1888:127652] 中國,香港特別行政區 2015-10-20 13:37:01.007 01-CLLocation[1888:127652] ( "<+22.28468100,+114.15817700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 15/10/20 \U4e2d\U56fd\U6807\U51c6\U65f6\U95f4\U4e0b\U53481:37:01" ) 2015-10-20 13:37:01.043 01-CLLocation[1888:127652] 中國,香港特別行政區 2015-10-20 13:37:01.443 01-CLLocation[1888:127652] 中國,香港特別行政區 2015-10-20 13:37:02.007 01-CLLocation[1888:127652] ( "<+22.28468100,+114.15817700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 15/10/20 \U4e2d\U56fd\U6807\U51c6\U65f6\U95f4\U4e0b\U53481:37:02" ) 2015-10-20 13:37:02.036 01-CLLocation[1888:127652] 中國,香港特別行政區
触摸开始后,将设置的两个位置显示在地图上的截图为: