iOS LBS CoreLocation定位,旋转角度,区域检测,地理编码,框架INTULocationManager使用
来源:互联网 发布:知乐的小说百度云 编辑:程序博客网 时间:2024/06/15 19:14
这里主要介绍CoreLocation的使用:
导入框架
导入主头文件
#import <CoreLocation/CoreLocation.h>
当然导入头文件框架会自动导入
CoreLocation框架中所有数据类型的前缀都是CL
CoreLocation中使用CLLocationManager对象来做用户定位
代码应用:
#import "ViewController.h"#import <CoreLocation/CoreLocation.h>@interface ViewController ()<CLLocationManagerDelegate>/** * 定位管理者 */@property (nonatomic ,strong) CLLocationManager *mgr;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // 成为CoreLocation管理者的代理监听获取到的位置 self.mgr.delegate = self; // 设置多久获取一次// self.mgr.distanceFilter = 500; // 设置获取位置的精确度 /* kCLLocationAccuracyBestForNavigation 最佳导航 kCLLocationAccuracyBest; 最精准 kCLLocationAccuracyNearestTenMeters; 10米 kCLLocationAccuracyHundredMeters; 百米 kCLLocationAccuracyKilometer; 千米 kCLLocationAccuracyThreeKilometers; 3千米 */// self.mgr.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; /* 注意: iOS7只要开始定位, 系统就会自动要求用户对你的应用程序授权. 但是从iOS8开始, 想要定位必须先"自己""主动"要求用户授权 在iOS8中不仅仅要主动请求授权, 而且必须再info.plist文件中配置一项属性才能弹出授权窗口 NSLocationWhenInUseDescription,允许在前台获取GPS的描述 NSLocationAlwaysUsageDescription,允许在后台获取GPS的描述 */ // 判断是否是iOS8 if([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) { NSLog(@"是iOS8"); // 主动要求用户对我们的程序授权, 授权状态改变就会通知代理 // [self.mgr requestAlwaysAuthorization]; // 请求前台和后台定位权限// [self.mgr requestWhenInUseAuthorization];// 请求前台定位权限 }else { NSLog(@"是iOS7"); // 3.开始监听(开始获取位置) [self.mgr startUpdatingLocation]; } }/** * 授权状态发生改变时调用 * * @param manager 触发事件的对象 * @param status 当前授权的状态 */- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ /* 用户从未选择过权限 kCLAuthorizationStatusNotDetermined 无法使用定位服务,该状态用户无法改变 kCLAuthorizationStatusRestricted 用户拒绝该应用使用定位服务,或是定位服务总开关处于关闭状态 kCLAuthorizationStatusDenied 已经授权(废弃) kCLAuthorizationStatusAuthorized 用户允许该程序无论何时都可以使用地理信息 kCLAuthorizationStatusAuthorizedAlways 用户同意程序在可见时使用地理位置 kCLAuthorizationStatusAuthorizedWhenInUse */ if (status == kCLAuthorizationStatusNotDetermined) { NSLog(@"等待用户授权"); }else if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) { NSLog(@"授权成功"); // 开始定位 [self.mgr startUpdatingLocation]; }else { NSLog(@"授权失败"); }}#pragma mark - CLLocationManagerDelegate//- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation/** * 获取到位置信息之后就会调用(调用频率非常高) * * @param manager 触发事件的对象 * @param locations 获取到的位置 */- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ NSLog(@"%s", __func__); // 如果只需要获取一次, 可以获取到位置之后就停止// [self.mgr stopUpdatingLocation]; // 1.获取最后一次的位置 /* location.coordinate; 坐标, 包含经纬度 location.altitude; 设备海拔高度 单位是米 location.course; 设置前进方向 0表示北 90东 180南 270西 location.horizontalAccuracy; 水平精准度 location.verticalAccuracy; 垂直精准度 location.timestamp; 定位信息返回的时间 location.speed; 设备移动速度 单位是米/秒, 适用于行车速度而不太适用于不行 */ /* 可以设置模拟器模拟速度 bicycle ride 骑车移动 run 跑动 freeway drive 高速公路驾车 */ CLLocation *location = [locations lastObject]; NSLog(@"%f, %f speed = %f", location.coordinate.latitude , location.coordinate.longitude, location.speed);}#pragma mark - 懒加载- (CLLocationManager *)mgr{ if (!_mgr) { _mgr = [[CLLocationManager alloc] init]; } return _mgr;}@end记得要再info.plist中配置下面两者选一
NSLocationWhenInUseDescription,允许在前台获取GPS的描述NSLocationAlwaysUsageDescription,允许在后台获取GPS的描述
后面说明随便写
获取用户角度:
<span style="font-size:12px;">#pragma mark - CLLocationManagerDelegate// 当获取到用户方向时就会调用- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{// NSLog(@"%s", __func__); /* magneticHeading 设备与磁北的相对角度 trueHeading 设置与真北的相对角度, 必须和定位一起使用, iOS需要设置的位置来计算真北 真北始终指向地理北极点 */// NSLog(@"%f", newHeading.magneticHeading); // 将获取到的角度转为弧度 = (角度 * π) / 180; CGFloat angle = newHeading.magneticHeading * M_PI / 180; // 如果需要根据角度来旋转图片,如指南针等,可以这样调用 /* 顺时针 正 逆时针 负数 */// </span><span style="font-family: Arial, Helvetica, sans-serif;">compasspointer为图片</span><span style="font-size:12px;"> self.compasspointer.transform = CGAffineTransformMakeRotation(-angle); }</span>
区域检测:
<span style="font-size:12px;"> // 创建中心点 CLLocationCoordinate2D center = CLLocationCoordinate2DMake(40.058501, 116.304171); // c创建圆形区域, 指定区域中心点的经纬度, 以及半径 CLCircularRegion *circular = [[CLCircularRegion alloc] initWithCenter:center radius:500 identifier:@"北京"]; [self.mgr startMonitoringForRegion:circular];</span>区域检测回调:
// 进入监听区域时调用- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{ NSLog(@"进入监听区域时调用");}// 离开监听区域时调用- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{ NSLog(@"离开监听区域时调用");}
地理编码:
<span style="font-size:12px;">// 根据传入的地址获取该地址对应的经纬度信息 CLGeocoder *geocoder = [[CLGeocoder alloc] init]</span><pre name="code" class="objc"><span style="font-size:12px;"> </span><pre name="code" class="objc" style="font-size:12px;"><span style="font-size: 12px;"><span style="white-space:pre"></span>NSString *addressStr = </span><span class="s1" style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">[[</span><span class="s2" style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">NSString</span><span class="s1" style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;"> </span><span class="s3" style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">alloc</span><span class="s1" style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;"> ]</span><span class="s3" style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">initWithString</span><span class="s1" style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">:</span><span class="s4" style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">@"</span><span class="s5" style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">北京</span><span class="s4" style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">"</span><span class="s1" style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">];</span>
<span style="font-size: 12px;"><span style="white-space:pre"></span>[geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) {</span>
<span style="font-size:12px;">if (placemarks.count == 0 || error != nil) { return ; } </span>
<span style="font-size:12px;">// placemarks地标数组, 地标数组中存放着地标, 每一个地标包含了该位置的经纬度以及城市/区域/国家代码/邮编等等... </span>
<span style="font-size:12px;">// 获取数组中的第一个地标 CLPlacemark *placemark = [placemarks firstObject];</span>
<span style="font-size:12px;">// for (CLPlacemark *placemark in placemarks) {</span>
<span style="font-size:12px;">// NSLog(@"%@ %@ %f %f", placemark.name, placemark.addressDictionary, placemark.location.coordinate.latitude, placemark.location.coordinate.longitude); </span>
<span style="font-size:12px;">self.latitudeLabel.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.latitude]; </span>
<span style="font-size:12px;">self.longitudeLabel.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.longitude];</span>
<span style="font-size:12px;"> NSArray *address = placemark.addressDictionary[@"FormattedAddressLines"]; </span>
<span style="font-size:12px;">NSMutableString *strM = [NSMutableString string]; for (NSString *str in address) { [strM appendString:str]; } NSLog(@"%@",strM); }];</span>
反地理编码:
// 根据用户输入的经纬度创建CLLocation对象 CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude doubleValue] longitude:[longtitude doubleValue]]; // 根据CLLocation对象获取对应的地标信息 [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { for (CLPlacemark *placemark in placemarks) { NSLog(@"%@ %@ %f %f", placemark.name, placemark.addressDictionary, placemark.location.coordinate.latitude, placemark.location.coordinate.longitude); self.reverseDetailAddressLabel.text = placemark.locality; } }];
开源框架INTULocationManager使用:
#import "ViewController.h"#import "INTULocationManager.h"#import <CoreLocation/CoreLocation.h>@interface ViewController ()@property (nonatomic, strong) CLLocationManager *mgr;@end@implementation ViewController- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ // 1.创建位置管理者 INTULocationManager *mgr = [INTULocationManager sharedInstance]; // 2.利用位置管理者获取位置 [mgr requestLocationWithDesiredAccuracy:INTULocationAccuracyRoom timeout:5 delayUntilAuthorized:YES block:^(CLLocation *currentLocation, INTULocationAccuracy achievedAccuracy, INTULocationStatus status) { if (status == INTULocationStatusSuccess) { NSLog(@"获取位置成功 %f %f", currentLocation.coordinate.latitude , currentLocation.coordinate.longitude); }else if(status == INTULocationStatusError) { NSLog(@"获取失败"); } }];}@end注意:INTULocationManager如果在8.0以后使用也需要在info.plist中配置
0 0
- iOS LBS CoreLocation定位,旋转角度,区域检测,地理编码,框架INTULocationManager使用
- iOS反地理编码,lbs定位
- iOS CoreLocation地理编码
- 地图定位,CoreLocation地理编码
- iOS开发 - CoreLocation地理定位
- iOS--CoreLocation地理反地理编码
- CoreLocation的使用 定位 既地理编码与反地理编码
- iOS CoreLocation框架 定位
- CoreLocation框架的基本使用 - 地理编码和反地理编码
- iOS 使用CoreLocation定位
- iOS-使用CoreLocation定位
- iOS开发—03CoreLocation地理编码
- (七十八)使用第三方框架INTULocationManager实现定位
- iOS中的定位-CoreLocation框架
- CoreLocation地理编码
- iOS—使用CoreLocation框架获取定位信息
- iOS开发拓展篇—CoreLocation地理编码
- iOS开发拓展篇—CoreLocation地理编码
- C++运算符重载(友元函数方式)
- php缓存机制
- 日经社説 20150710 農協の自己改革を期待する
- eclipse菜单没有Android Virtual Device Manager
- caffe学习网页推荐
- iOS LBS CoreLocation定位,旋转角度,区域检测,地理编码,框架INTULocationManager使用
- UIMenuController
- Swift教程之结构体
- 10个免费的服务器监控工具
- NYoj89 汉诺塔(二)
- 日经社説 20150710 この新国立競技場を未来へ引き渡せるか
- 重写String类
- 10 款免费的网络监控工具
- 在Xcode6上创建pch文件