IOS之定位详解
来源:互联网 发布:mac 数据库建模软件 编辑:程序博客网 时间:2024/05/23 23:00
//1.导入框架
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
{
//2.声明全局的定位管理器对象
CLLocationManager *manager;
float allTime;
float allDistance;
CLLocation *lastLocation;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
/*
1.CoreLocation框架
2.CLLocationManager:定位管理器
3.CLocation:保存位置信息
*/
//3.实例化定位管理器对象
manager = [[CLLocationManageralloc]init];
//4.判断手机是否打开定位服务
BOOL isOpenLocationService = [CLLocationManagerlocationServicesEnabled];
if (isOpenLocationService ==NO) {
NSLog(@"请到设置->隐私->定位服务里打开定位服务");
return;
}
//5.获取定位服务权限 (并且到info.plist进行相应配置,加一个key值,和下面对应
NSLocationWhenInUseUsageDescription
还有一种是
NSLocationAlwaysUsageDescription,那么对应下面应该是
[manager requestAlwaysAuthorization];
)[manager requestWhenInUseAuthorization];
//6.设置定位精度和频率
//精度
manager.desiredAccuracy =10;
//频率
manager.distanceFilter =10;
//7.挂代理
manager.delegate =self;
//8.开始定位
[managerstartUpdatingLocation];
}
//实现代理方法,定位成功
static int isFirst =0;
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
//获取到代表当前位置信息的CLLocation对象
CLLocation *curLocation = locations.lastObject;
NSLog(@"经度=%f \n 纬度=%f",curLocation.coordinate.latitude,curLocation.coordinate.longitude);
NSLog(@"海拔%f",curLocation.altitude);
if (isFirst ==0) {
isFirst =1;
}else{
float istance = [curLocationdistanceFromLocation:lastLocation];
allDistance +=istance;
float time = [curLocation.timestamptimeIntervalSinceDate:lastLocation.timestamp];
allTime += time;
}
NSLog(@"行驶了%fm行驶了%fs速度%fm/s",allDistance,allTime,allDistance/allTime);
lastLocation = curLocation;
}
//定位失败,返回错误原因
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"%@",error);
}
附录编码反编码的方法
- (void)viewDidLoad {
[superviewDidLoad];
/*
* 编码与反编码
* CLGeocoder ->CoreLocation框架,实现编码与反编码
* CLPlaceMark -> CoreLocation框架,存放地标信息
* PS:编码与反编码较耗性能,建议放到子线程执行
*/
[selfgeocoder:@"郑州科技学院"];
CLLocationCoordinate2D coor ;
coor.latitude =31;
coor.longitude =113;
[selfreverseCoder:coor];
}
//编码
-(void)geocoder:(NSString *)name{
dispatch_queue_t queue =dispatch_get_global_queue(0,0);
dispatch_async(queue, ^{
//创建一个编码对象
CLGeocoder *geocoder = [[CLGeocoderalloc]init];
//编码
[geocoder geocodeAddressString:namecompletionHandler:^(NSArray<CLPlacemark *> *_Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placeMark = placemarks.lastObject;
NSLog(@"经纬度=%f %f",placeMark.location.coordinate.longitude,placeMark.location.coordinate.latitude);
NSLog(@"地址信息字典=%@",placeMark.addressDictionary);
}];
});
}
//反编码
-(void)reverseCoder:(CLLocationCoordinate2D )coor{
dispatch_queue_t queue =dispatch_get_global_queue(0,0);
dispatch_async(queue, ^{
CLGeocoder *reverseGeocoder = [[CLGeocoderalloc]init];
CLLocation *location = [[CLLocationalloc]initWithLatitude:coor.latitudelongitude:coor.longitude];
[reverseGeocoder reverseGeocodeLocation:locationcompletionHandler:^(NSArray<CLPlacemark *> *_Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placemark = placemarks.lastObject;
NSLog(@"%@",placemark.addressDictionary);
}];
});
}
- IOS之定位详解
- ios定位服务详解
- iOS定位,地图详解
- CoreLocation IOS 定位详解
- iOS定位详解
- iOS学习之定位
- css之定位详解
- iOS 详解CoreLocation定位服务
- iOS 定位服务编程详解
- iOS 定位服务编程详解
- iOS 定位服务编程详解
- iOS 定位方式 iOSNsPredicateString 详解
- Ios开发之定位CLLocationManager
- iOS开发之定位CLLocationManager
- iOS开发之地图定位
- iOS之地图与定位
- iOS学习笔记之-定位
- iOS开发笔记之定位
- C++ 引用类型const形参的用法
- PHP官方网站及PHP手册
- AndroidUI--ActionBar(Menu)
- 使用org.apache.commons.codec做md5&SHA1&BASE64加密
- 第十一周上机实践项目 项目4-教师兼职干部类
- IOS之定位详解
- ubuntu 的使用 —— 常用命令
- android Service的启动方式和重复绑定
- ptrace运行原理及使用详解
- POJ 1001 Exponentiation&&NYOJ 155 求高精度幂
- 配置基于UEFI的硬盘启动WinPE
- HDU 5249 KPI 二分查找
- 【Linux】redis集群
- Integer判等的陷阱:你知道Integer内部高速缓冲区IntegerCache吗?