地图定位
2016-05-31 17:25
447 查看
#import "ViewController.h" // 第一步:引入库头文件 #import <CoreLocation/CoreLocation.h> @interface ViewController () <CLLocationManagerDelegate> /** 定位管理器 */ @property (nonatomic, strong) CLLocationManager *manger; //CoreLocation框架中CLLocationManager用于管理定位的管理器 /** 编码与反编码的类 */ @property (nonatomic, strong) CLGeocoder *geocoder; //CoreLocation框架中CLGeocoder用于编码和反编码 @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // NSLocationWhenInUseUsageDescription来告诉用户使用定位服务的目的,并且注意这个配置是必须的,如果不进行配置则默认情况下应用无法使用定位服务,打开应用不会给出打开定位服务的提示,除非安装后自己设置此应用的定位服务。同时,在应用程序中需要根据配置对requestAlwaysAuthorization或locationServicesEnabled方法进行请求。由于本人机器已经更新到最新的iOS8.1下面的内容主要针对iOS8,使用iOS7的朋友需要稍作调整。 // 定位的步骤: // 1.初始化定位管理器 self.manger = [[CLLocationManager alloc] init]; // 2.进行隐私的判断并授权 // 进行隐私的判断 if (![CLLocationManager locationServicesEnabled]) { NSLog(@"是否前往隐私进行设置允许定位"); [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=privacy"]]; } // 根据状态进行授权 // 进行版本的判断 if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 8.0) { /* 定位服务授权状态,返回枚举类型: kCLAuthorizationStatusNotDetermined: 用户尚未做出决定是否启用定位服务 kCLAuthorizationStatusRestricted: 没有获得用户授权使用定位服务,可能用户没有自己禁止访问授权 kCLAuthorizationStatusDenied :用户已经明确禁止应用使用定位服务或者当前系统定位服务处于关闭状态 kCLAuthorizationStatusAuthorizedAlways: 应用获得授权可以一直使用定位服务,即使应用不在使用状态 kCLAuthorizationStatusAuthorizedWhenInUse: 使用此应用过程中允许访问定位服务 */ /* 在授权请求之前需要在inforPlist中设置允许定位的内容【需要在info.plist文件中添加的,设置提示语】 NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription */ if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) { // 请求授权 [self.manger requestWhenInUseAuthorization]; } } // 3.设置管理器的代理和相关属性 self.manger.delegate = self; // 设置经度 self.manger.desiredAccuracy = 100; // 设置最小更新距离 self.manger.distanceFilter = 100; // 4.开启定位 [self.manger startUpdatingLocation]; /************************************ 编码与反编码 ****************************************/ // 初始化对象 self.geocoder = [[CLGeocoder alloc] init]; // 根据地名获取经纬度 [self getCoordinateByAdress:@"北京"]; // 根据经纬度反编码取出地名 [self getAddressByLatitude:34 Longitude:113]; // 计算两点之间的距离 [self distance]; } #pragma mark - 计算两点之间的距离 - (void)distance { // 创建两个位置 CLLocation *locationBeijing = [[CLLocation alloc] initWithLatitude:40 longitude:116]; CLLocation *locationDalian = [[CLLocation alloc] initWithLatitude:39 longitude:121]; CLLocationDistance distance = [locationBeijing distanceFromLocation:locationDalian]; NSLog(@"北京到大连的距离:%f", distance); } #pragma mark - 根据经纬度反编码取出地名 - (void)getAddressByLatitude:(CLLocationDegrees)latitude Longitude:(CLLocationDegrees)longitude { // 反编码 // 创建CLLocation CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { NSDictionary *dic = placemarks.firstObject.addressDictionary; NSLog(@"反编码地理位置信息:%@", dic); }]; } #pragma mark - 根据地名获取相关的信息 - (void)getCoordinateByAdress:(NSString *)address { // 编码方法 [_geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { // 根据返回的地标,取出第一个位置(地标的位置很多) CLPlacemark *mark = placemarks.firstObject; // 根据地标得到location CLLocation *location = mark.location; // 根据mark获取区域 CLRegion *region = mark.region; // 获取字典信息 NSDictionary *addressDic = mark.addressDictionary; NSLog(@"地标位置:%@, 区域:%@, 地理位置信息:%@", location, region, addressDic); // NSString *name=placemark.name;//地名 // NSString *thoroughfare=placemark.thoroughfare;//街道 // NSString *subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等 // NSString *locality=placemark.locality; // 城市 // NSString *subLocality=placemark.subLocality; // 城市相关信息,例如标志性建筑 // NSString *administrativeArea=placemark.administrativeArea; // 州 // NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政区域信息 // NSString *postalCode=placemark.postalCode; //邮编 // NSString *ISOcountryCode=placemark.ISOcountryCode; //国家编码 // NSString *country=placemark.country; //国家 // NSString *inlandWater=placemark.inlandWater; //水源、湖泊 // NSString *ocean=placemark.ocean; // 海洋 // NSArray *areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标 }]; } #pragma mark - CLLocationManagerDelegate的代理方法 // 定位成功之后开始更新位置信息,移动设置的最小距离之后也开始调用这个方法 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations { // 获取最后一次的位置 CLLocation *location = locations.lastObject; // 获取位置 CLLocationCoordinate2D coordinate = location.coordinate; // 打印经度, 纬度, 海拔, 航海方向, 移动速度 NSLog(@"经度:%f, 纬度:%f, 海拔:%f, 航海方向:%f, 移动速度:%f", coordinate.longitude, coordinate.latitude, location.altitude, location.course, location.speed); // 为了节省电源,如果不使用定位,需要把定位关掉 [self.manger stopUpdatingLocation]; } // 定位失败 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"定位失败"); } @end
如果定位失败,在模拟器的debug中设置一下,可能就好了
相关文章推荐
- iOS \'The sandbox is not sync with the Podfile.lock\'问题解决
- makesfx.exe (Make SFX (Self-extracting archive))
- Android组件ViewStub基本使用方法详解
- java中日期格式的加减,Timestamp和util.Date格式互转
- MYSQL 定时执行存储过程
- EditText常用属性
- Android自动化测试之Robotium学习(四)
- SharedPreference.Editor的apply和commit方法异同
- iOS 如何检测应用更新?
- 漂亮的代码5:数组与字符一样的操作
- android 蓝牙连接库(蓝牙设备选择器)
- 得到json 中的key 值
- 计算机网络学习(4):网络协议与标准
- UILabel自动换行+自适应高度
- 图像处理-线性滤波-3 高斯滤波器
- C#一般处理程序 ashx.cs使用Session报错的处理
- 加一
- C语言练习篇-2交换两个变量的值
- centos安装MYSQL 5.6
- VS快捷键使用总结