IOS-CoreLocation
一、简介
typedef struct {
CLLocationDegrees latitude; // 纬度
CLLocationDegrees longitude; // 经度
} CLLocationCoordinate2D;
//
// ViewController.m
// IOS_0403_导航
//
// Created by ma c on 16/4/3.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "ViewController.h"
#import <CoreLocation/CoreLocation.h> @interface ViewController ()<CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager *mgr; //上一次位置
@property (nonatomic, strong) CLLocation *previousLocation;
//总路程
@property (nonatomic, assign) CLLocationDistance sumDistance;
//总时间
@property (nonatomic, assign) NSTimeInterval sumTime; @property (nonatomic, strong) UIImageView *commpasspointer; @end @implementation ViewController /*
注意:iOS7只要定位,系统就会自动要求用户对你的应用程序授权,但是从iOS8开始想要定位
必须自己主动要求用户授权,然后在Info.plist文件中配置一项属性才能弹出授权窗口
NSLocationWhenInUseDescription,允许在前台获取GPS的描述
NSLocationAlwaysUsageDescription,允许在后台获取GPS的描述 CLLocation
location.coordinate; 坐标, 包含经纬度
location.altitude; 设备海拔高度 单位是米
location.course; 设置前进方向 0表示北 90东 180南 270西
location.horizontalAccuracy; 水平精准度
location.verticalAccuracy; 垂直精准度
location.timestamp; 定位信息返回的时间
location.speed; 设备移动速度 单位是米/秒, 适用于行车速度而不太适用于不行 CLHeading
magneticHeading 设备与磁北的相对角度
trueHeading 设置与真北的相对角度, 必须和定位一起使用, iOS需要设置的位置来计算真北
真北始终指向地理北极点
磁北对应随着时间变化的地球磁场北极 CLRegion 代表一个区域
>CLCircularRegion 圆形区域
>CLBeaconRegion 蓝牙信号区域
*/ - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor]; [self test]; //添加指南针图片
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_compasspointer"]];
imgView.center = self.view.center;
[self.view addSubview:imgView];
self.commpasspointer = imgView;
} - (void)test
{
//1.创建管理者
// CLLocationManager *mgr = [[CLLocationManager alloc] init];
//2.设置代理
self.mgr.delegate = self; //设置获取位置精确度
self.mgr.desiredAccuracy = kCLLocationAccuracyBest;
//设置多久获取一次
self.mgr.distanceFilter = ; if ([[UIDevice currentDevice].systemVersion doubleValue] > 8.0) {
//主动请求授权
[self.mgr requestAlwaysAuthorization];
} //3.开始定位
[self.mgr startUpdatingLocation]; //3.开始获取用户方向
[self.mgr startUpdatingHeading]; //3.开始区域检测
//创建中心点
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(, );
//创建圆形区域
CLCircularRegion *circularRegion = [[CLCircularRegion alloc] initWithCenter:center radius: identifier:@"北京"];
[self.mgr startMonitoringForRegion:circularRegion]; } #pragma mark - CLLocationManagerDelegate
//当获取到位置时调用
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
//获取当前位置
CLLocation *newLocation = [locations lastObject]; NSLog(@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude); if (self.previousLocation == nil) { //计算两次之间的位置(单位为米)
CLLocationDistance distance = [newLocation distanceFromLocation:self.previousLocation];
//计算两次之间的时间(单位为秒)
NSTimeInterval dTime = [newLocation.timestamp timeIntervalSinceDate:self.previousLocation.timestamp];
//计算速度
CGFloat speed = distance / dTime; //累加时间
self.sumTime += dTime;
//累加路程
self.sumDistance += distance;
//计算平均速度
CGFloat averSpeed = self.sumDistance / self.sumTime;
}
//记录上一次位置
self.previousLocation = newLocation; } //当获取到用户方向时调用
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
//1.将角度转换成弧度
CGFloat angle = newHeading.magneticHeading * M_PI / ;
//2.旋转图片
self.commpasspointer.transform = CGAffineTransformIdentity;
self.commpasspointer.transform = CGAffineTransformMakeRotation(-angle);
} //进入监听区域时调用
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"didEnterRegion");
} //离开监听区域时调用
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(@"didExitRegion");
} #pragma mark - 懒加载
- (CLLocationManager *)mgr
{
if(!_mgr){
_mgr = [[CLLocationManager alloc] init]; }
return _mgr;
} @end
地理编码
//
// ViewController.m
// IOS_0403_地理编码
//
// Created by ma c on 16/4/3.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "ViewController.h"
#import <CoreLocation/CoreLocation.h> @interface ViewController ()
//-------------------正向地理编码----------------//
/**
* 监听地理编码点击事件
*/
- (IBAction)geocodeBtnClick;
/**
* 需要编码的地址容器
*/
@property (weak, nonatomic) IBOutlet UITextField *addressField;
/**
* 经度容器
*/
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
/**
* 纬度容器
*/
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
/**
* 详情容器
*/
@property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel;
/**
* 地理编码对象
*/ //-------------------反向地理编码----------------//
- (IBAction)reverseGeocode;
@property (weak, nonatomic) IBOutlet UITextField *longtitudeField;
@property (weak, nonatomic) IBOutlet UITextField *latitudeField;
@property (weak, nonatomic) IBOutlet UILabel *reverseDetailAddressLabel; @property (nonatomic ,strong) CLGeocoder *geocoder; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
}
//正向地理编码
- (IBAction)geocodeBtnClick
{
// 0.获取用户输入的位置
NSString *addressStr = self.addressField.text;
if (addressStr == nil || addressStr.length == ) {
NSLog(@"请输入地址");
return;
} // 1.创建地理编码对象 // 2.利用地理编码对象编码
// 根据传入的地址获取该地址对应的经纬度信息
[self.geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) { if (placemarks.count == || error != nil) {
return ;
}
// placemarks地标数组, 地标数组中存放着地标, 每一个地标包含了该位置的经纬度以及城市/区域/国家代码/邮编等等...
// 获取数组中的第一个地标
CLPlacemark *placemark = [placemarks firstObject];
// for (CLPlacemark *placemark in placemarks) {
// NSLog(@"%@ %@ %f %f", placemark.name, placemark.addressDictionary, placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);
self.latitudeLabel.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.latitude];
self.longitudeLabel.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.longitude];
NSArray *address = placemark.addressDictionary[@"FormattedAddressLines"];
NSMutableString *strM = [NSMutableString string];
for (NSString *str in address) {
[strM appendString:str];
}
self.detailAddressLabel.text = strM;
// } }];
}
//反向地理编码
- (IBAction)reverseGeocode
{
// 1.获取用户输入的经纬度
NSString *longtitude = self.longtitudeField.text;
NSString *latitude = self.latitudeField.text;
if (longtitude.length == ||
longtitude == nil ||
latitude.length == ||
latitude == nil) {
NSLog(@"请输入经纬度");
return;
} // 2.根据用户输入的经纬度创建CLLocation对象
CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude doubleValue] longitude:[longtitude doubleValue]]; // 3.根据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;
}
}];
} #pragma mark - 懒加载
- (CLGeocoder *)geocoder
{
if(!_geocoder){
_geocoder = [[CLGeocoder alloc] init]; }
return _geocoder;
} @end
IOS-CoreLocation的更多相关文章
- IOS CoreLocation框架的使用(用于地理定位)
● 在移动互联网时代,移动app能解决用户的很多生活琐事,比如 ● 导航:去任意陌生的地方 ● 周边:找餐馆.找酒店.找银行.找电影院 ● 在上述应用中,都用到了地图和定位功能,在iOS开发中 ...
- iOS - CoreLocation 定位
前言 NS_CLASS_AVAILABLE(10_6, 2_0) @interface CLLocationManager : NSObject 1.CoreLocation 定位 配置 1.在 iO ...
- IOS深入学习
iOS超全开源框架.项目和学习资料汇总(1)UI篇 iOS超全开源框架.项目和学习资料汇总(2)动画篇 iOS超全开源框架.项目和学习资料汇总(3)网络和Model篇 数据库 FMDB – sqlit ...
- iOS开发——高级篇——地理定位 CoreLocation
一.CoreLocation 在移动互联网时代,移动app能解决用户的很多生活琐事,比如周边:找餐馆.找KTV.找电影院等等导航:根据用户设定的起点和终点,进行路线规划,并指引用户如何到达 在上述应用 ...
- iOS开发拓展篇—CoreLocation简单介绍
iOS开发拓展篇—CoreLocation简单介绍 一.简介 1.在移动互联网时代,移动app能解决用户的很多生活琐事,比如 (1)导航:去任意陌生的地方 (2)周边:找餐馆.找酒店.找银行.找电影院 ...
- iOS开发拓展篇—CoreLocation定位服务
iOS开发拓展篇—CoreLocation定位服务 一.简单说明 1.CLLocationManager CLLocationManager的常用操作和属性 开始用户定位- (void)startUp ...
- iOS开发拓展篇—CoreLocation地理编码
iOS开发拓展篇—CoreLocation地理编码 一.简单说明 CLGeocoder:地理编码器,其中Geo是地理的英文单词Geography的简写. 1.使用CLGeocoder可以完成“地理编码 ...
- iOS:地图:MapKit和CoreLocation
地图:MapKit和CoreLocation 简介: 现在很多的社交软件都引入了地图和定位功能,要想实现这2大功能,那就不得不学习其中的2个框架:MaKit和CoreLocation CoreLoca ...
- 【iOS】7.4 定位服务->2.1.1 定位 - 官方框架CoreLocation: 请求用户授权
本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...
- 【iOS】7.4 定位服务->2.1.2 定位 - 官方框架CoreLocation: CLLocationManager(位置管理器)
本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...
随机推荐
- MongoDB-1:安装和配置
一.简介 MongoDB一种非关系型数据库(NoSql),是一种强大.灵活.可扩展的数据存储方式,因为MongoDB是文档模型,自由灵活很高,可以让你在开发过程中畅顺无比,对于大数据量.高并发.弱事务 ...
- Java集合的遍历方式
Map的遍历 1.通过map.entrySet遍历Key和Value Map<Integer,Integer> map = new HashMap<>(); map.put(1 ...
- Java AES512加密算法
AES - 高级加密标准: 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这 ...
- 【转】Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- 牛客网暑期ACM多校训练营(第一场)- J Different Integers (莫队)
题意:裸的莫队题,每个查询Li,Ri,返回区间[1,Li]和[Ri,N]区间中不同的数的个数. 分析:正常的离线查询,是求区间[Li,Ri]中要求的答案,而该题所求答案为外侧两个区间中的答案,那么cn ...
- HDU 6319 Ascending Rating (单调双端队列)
题意:给定一个序列a[1..n],对于每个长度为m的连续子区间,求出区间的最大值和从左往右扫描该区间最大值的变化次数. 分析:先O(n)处理出整个序列的值.求出每个长度为m的连续区间中的最大值可以用单 ...
- MyEclipse激活失败
最近从MyEclipse2014升级MyEclipse2015,结果按照MyEclipse2014的方式激活2015总是失败,显示错误如下图所示: 反复实验,怎么也不能成功激活,最终找到方法 很多情况 ...
- 抗DDOS攻击
2016年10月的某天,“半个美国互联网”都瘫痪了,就是因为遭受了DDoS攻击——Twitter.GitHub.Spotify.Airbnb.Etsy等大量站点都因此受到影响.DNS服务提供商Dyn公 ...
- 在Linux系统中使用蓝牙功能的基本方法
首先确定硬件上有支持蓝牙的设备,然后运行如下命令,就可以开到我们的蓝牙设备了: lsusb 运行hciconfig可以看到:从上图可以看出,我们的蓝牙设备是hci0运行hcitool dev可以看到我 ...
- 初涉Rx套餐 之RxBinding(让你的事件流程更清晰)
转载请注明出处:王亟亟的大牛之路 最近下班回家都在WOW,周末就爆肝,感觉人都要GO DIE了,昨天下午看了看RxBinding相关的功能感觉还是蛮强大的,所提供的API也是相当丰富(基本Rx套餐都是 ...