IOS8 CoreLocation
来源:互联网 发布:综合布线仿真软件下载 编辑:程序博客网 时间:2024/05/16 11:07
IOS8 CoreLocation 的使用
*
在 info.plist里加入:
NSLocationWhenInUseDescription,允许在前台获取GPS的描述
NSLocationAlwaysUsageDescription,允许在后台获取GPS的描述
*/
[self.mgr requestAlwaysAuthorization];//手动申请获取位置的权限
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ /** * kCLAuthorizationStatusNotDetermined = 0,//等待授权 // This application is not authorized to use location services. Due // to active restrictions on location services, the user cannot change // this status, and may not have personally denied authorization kCLAuthorizationStatusRestricted, //无法使用定位服务,此状态用户无法修改 // User has explicitly denied authorization for this application, or // location services are disabled in Settings. kCLAuthorizationStatusDenied, //拒绝权限 // User has granted authorization to use their location at any time, // including monitoring for regions, visits, or significant location changes. kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(NA, 8_0), //前台后台都可以访问位置的权限 // User has granted authorization to use their location only when your app // is visible to them (it will be made visible to them if you continue to // receive location updates while in the background). Authorization to use // launch APIs has not been granted. kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0), //只有在前台才能访问位置的权限 // This value is deprecated, but was equivalent to the new -Always value. kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0, "Use kCLAuthorizationStatusAuthorizedAlways") = kCLAuthorizationStatusAuthorizedAlways */ if (status == kCLAuthorizationStatusNotDetermined) { NSLog(@"等待授权!"); } else if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) { NSLog(@"授权成功!"); [self.mgr startUpdatingLocation]; } else { NSLog(@"授权失败"); }}-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ //打印位置信息 NSLog(@"%@",locations); }//懒加载-(CLLocationManager *)mgr{ if (!_mgr) { _mgr = [[CLLocationManager alloc] init]; } return _mgr;}@end
locations的array信息的打印
在info.plist中添加如下配置
模拟器中便可以跳出提示框,点击allow,便会频繁打印定位信息,打印方法如上
二`
正确让ios8定位的方法来了:
1.在Info.plist表里面添加两个变量
NSLocationAlwaysUsageDes
NSLocationWhenInUseUsage
(这两个字段没什么特别的意思,就是自定义提示用户授权使用地理定位功能时的提示语)
2.添加代码:
CLLocationManager
[locationManager requestAlwaysAuthorizati
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
调用代理:
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationSt
switch (status) {
case kCLAuthorizationStatusNo
if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorizati
}
break;
default:
break;
}
3.这样就可以打开地图定位功能了!
最近在做基于百度Map定位及路线轨迹的功能!完成后,也发出来供大家学习使用,尽情期待吧~
(后续)
最近手机升级到ios8以上,发现地图又无法定位了!
各种求救,最后找到解决方法!
ios8对定位做了一些调整,
1.@interface里:
CLLocationManager *locationManager;
2.locationManager=[[CLLcationManager alloc]init];
[locationManager requestWhenInUseAuthoriz
[locationManager startUpdatingLocation];
三`
CoreLocation地理编码
一、简单说明
CLGeocoder:地理编码器,其中Geo是地理的英文单词Geography的简写。
1.使用CLGeocoder可以完成“地理编码”和“反地理编码”
地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
反地理编码:根据给定的经纬度,获得具体的位置信息
(1)地理编码方法
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
(2)反地理编码方法
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
2.CLGeocodeCompletionHandler
当地理\反地理编码完成时,就会调用CLGeocodeCompletionHandler
这个block传递2个参数
error :当编码出错时(比如编码不出具体的信息)有值
placemarks :里面装着CLPlacemark对象
3.CLPlacemark
说明:CLPlacemark的字面意思是地标,封装详细的地址位置信息
地理位置 @property (nonatomic, readonly) CLLocation *location;
区域 @property (nonatomic, readonly) CLRegion *region;
详细的地址信息 @property (nonatomic, readonly) NSDictionary *addressDictionary;
地址名称 @property (nonatomic, readonly) NSString *name;
城市 @property (nonatomic, readonly) NSString *locality;
二、代码示例:
在storyboard中搭建界面如下:
实现代码:
YYViewController.m文件
1 // 2 // YYViewController.m 3 // 19-地理编码 4 // 5 // Created by apple on 14-8-11. 6 // Copyright (c) 2014年 yangyong. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 #import <CoreLocation/CoreLocation.h> 11 12 @interface YYViewController () 13 @property(nonatomic,strong)CLGeocoder *geocoder; 14 #pragma mark-地理编码 15 - (IBAction)geocode; 16 @property (weak, nonatomic) IBOutlet UITextField *addressField; 17 @property (weak, nonatomic) IBOutlet UILabel *longitudeLabel; 18 @property (weak, nonatomic) IBOutlet UILabel *latitudeLabel; 19 @property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel; 20 21 #pragma mark-反地理编码 22 23 - (IBAction)reverseGeocode; 24 @property (weak, nonatomic) IBOutlet UITextField *longitudeField; 25 @property (weak, nonatomic) IBOutlet UITextField *latitudeField; 26 @property (weak, nonatomic) IBOutlet UILabel *reverdeDetailAddressLabel; 27 @end 28 29 @implementation YYViewController 30 31 #pragma mark-懒加载 32 -(CLGeocoder *)geocoder 33 { 34 if (_geocoder==nil) { 35 _geocoder=[[CLGeocoder alloc]init]; 36 } 37 return _geocoder; 38 } 39 - (void)viewDidLoad 40 { 41 [super viewDidLoad]; 42 } 43 /** 44 * 地理编码:地名—>经纬度坐标 45 */ 46 - (IBAction)geocode { 47 //1.获得输入的地址 48 NSString *address=self.addressField.text; 49 if (address.length==0) return; 50 51 //2.开始地理编码 52 //说明:调用下面的方法开始编码,不管编码是成功还是失败都会调用block中的方法 53 [self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) { 54 //如果有错误信息,或者是数组中获取的地名元素数量为0,那么说明没有找到 55 if (error || placemarks.count==0) { 56 self.detailAddressLabel.text=@"你输入的地址没找到,可能在月球上"; 57 }else // 编码成功,找到了具体的位置信息 58 { 59 //打印查看找到的所有的位置信息 60 /* 61 name:名称 62 locality:城市 63 country:国家 64 postalCode:邮政编码 65 */ 66 for (CLPlacemark *placemark in placemarks) { 67 NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@",placemark.name,placemark.locality,placemark.country,placemark.postalCode); 68 } 69 70 //取出获取的地理信息数组中的第一个显示在界面上 71 CLPlacemark *firstPlacemark=[placemarks firstObject]; 72 //详细地址名称 73 self.detailAddressLabel.text=firstPlacemark.name; 74 //纬度 75 CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude; 76 //经度 77 CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude; 78 self.latitudeLabel.text=[NSString stringWithFormat:@"%.2f",latitude]; 79 self.longitudeLabel.text=[NSString stringWithFormat:@"%.2f",longitude]; 80 } 81 }]; 82 } 83 84 /** 85 * 反地理编码:经纬度坐标—>地名 86 */ 87 - (IBAction)reverseGeocode { 88 //1.获得输入的经纬度 89 NSString *longtitudeText=self.longitudeField.text; 90 NSString *latitudeText=self.latitudeField.text; 91 if (longtitudeText.length==0||latitudeText.length==0) return; 92 93 CLLocationDegrees latitude=[latitudeText doubleValue]; 94 CLLocationDegrees longitude=[longtitudeText doubleValue]; 95 96 CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude]; 97 //2.反地理编码 98 [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { 99 if (error||placemarks.count==0) {100 self.reverdeDetailAddressLabel.text=@"你输入的地址没找到,可能在月球上";101 }else//编码成功102 {103 //显示最前面的地标信息104 CLPlacemark *firstPlacemark=[placemarks firstObject];105 self.reverdeDetailAddressLabel.text=firstPlacemark.name;106 //经纬度107 CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;108 CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;109 self.latitudeField.text=[NSString stringWithFormat:@"%.2f",latitude];110 self.longitudeField.text=[NSString stringWithFormat:@"%.2f",longitude];111 }112 }];113 }114 115 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event116 {117 [self.view endEditing:YES];118 }119 @end
实现效果:
(1)地理编码:(地名->经纬度坐标)
打印输出:
(2)反地理编码:(经纬度—>地名)
(3)注意:调整键盘
点击经纬度textField进行输入的时候,弹出的键盘如下
(4)注意:搜索的所有结果都是在中国境内的,因为苹果在中国的地图服务商是高德地图。
四`
CoreLocation中得到的定位信息都是以经度和纬度等表示的地理信息,很多时候我们需要把它反向编码成普通人能读懂的地理位置描述如:X国XX市XXX区XXX街道XX号,这就需要用到MapKit中的一个地理位置反向编码工具:MKReverseGeocoder
用法:
1,首先要实现协议MKReverseGeocoderDelegate,因为将坐标信息发到服务器再反回来需要一定的时间,所以为了防止阻塞,发出信息后并不知到什么时候会返回信息,信息返回时会通知委托方法。这里实现这个类主要时为了实现2个方法如下:
- - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{
- NSLog(@"MKReverseGeocoder has failed.");
- }
- - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
- NSLog(@"当前地理信息为:%@",placemark);
- }
2,然后可以init一个反向编码器,然后发出请求了:
- MKReverseGeocoder *reverseGeocoder =[[MKReverseGeocoder alloc] initWithCoordinate:coordinate];
- NSLog(@"%g",coordinate.latitude);
- NSLog(@"%g",coordinate.longitude);
- reverseGeocoder.delegate = self;
- [reverseGeocoder start];
- IOS8 CoreLocation
- iOS8地图定位-CoreLocation
- CoreLocation的变化-iOS8
- CoreLocation Authorization in iOS8+
- 迎接iOS8 - CoreLocation的变化
- iOS8中使用CoreLocation定位
- 迎接iOS8 - CoreLocation的变化
- iOS8中使用CoreLocation定位
- iOS8下,CoreLocation的变化
- iOS8中使用CoreLocation定位
- iOS8中使用CoreLocation定位
- iOS8中使用CoreLocation定位
- iOS8中使用CoreLocation定位
- iOS8中使用CoreLocation定位
- iOS8中使用CoreLocation定位
- iOS8中使用CoreLocation定位
- iOS8中使用CoreLocation定位
- iOS8中使用CoreLocation定位
- 129 - Krypton Factor
- 蓝桥杯 算法训练 最大的算式 (动态规划)
- leetcode_c++:Jump Game II(045)
- hdoj 2032杨辉三角 解题报告
- Problem-Q
- IOS8 CoreLocation
- socket关闭之close()和shutdown()的差异
- 计算机视觉CV领域大牛及研究组主页链接
- 实习入职第四天:Thread有关知识点
- 频域分析法求解零状态响应的matlab过程
- UVA 11324 scc缩点+树上最长路
- Linux C语言遍历文件夹
- Leetcode 217. Contains Duplicate
- HDU 2539 点球大战(字符串+输出坑)