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 设备工具 === ...
随机推荐
- MySQL给字段唯一索引的三种方法
建表时添加 DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `stu_id` ) NOT NULL AUTO_INCREMENT, ` ...
- python数据类型二(列表和元组)
一.列表 1.1 列表的介绍 列表是python的基本数据类型之一,其他编程语言也有类似的数据类型,比如JS中的数组,java中的数组等等,它是以[]括起来,每个元素用逗号隔开,而且可以存放各种数据类 ...
- Linux内核设计与实现——内核同步
内核同步 同步介绍 同步的概念 临界区:也称为临界段,就是訪问和操作共享数据的代码段. 竞争条件: 2个或2个以上线程在临界区里同一时候运行的时候,就构成了竞争条件. 所谓同步.事实上防止在临界区中形 ...
- docker的安装以及jdk和tomcat的环境配置
准备工作:需要Linux kernel 3.8支持查看linux内核的版本:root@ubuntu-dev:~# cat /proc/version查看linux版本:root@ubuntu-dev: ...
- 60. Permutation Sequence(求全排列的第k个排列)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- hdu1711 Number Sequence kmp应用
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目: Problem Description Given two sequences of n ...
- POJ 2253 Frogger (Dijkstra变型)
题意:求点1到点2的路径中,权值最大的那条边,其最小值是多少. 分析:最大值最小化.可以将迪杰斯特拉模板中的松弛操作加以修改,在O(n^2)的时间内解决该问题.其中需要注意的是,dist[i]指的是: ...
- bat脚本相关
前期准备: 将要执行的脚本名字生成到一个txt文件 首先进入dos运行程序的目录下:输入dir *.jmx /B>FileScript.txt 采用dir *.jmx>list.txt 如 ...
- [Cannot deserialize JSON array into type] NewtonSoft.Json解析数据出错原因
今天用NewtonSoft.JSon解析一个天气数据,数据格式如: {"status":1,"detail":"\u6570\u636e\u83b7\ ...
- CSS3:布局display属性的flex(弹性布局)
CSS3:布局display属性的flex(弹性布局) 一.简介 Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性.设为Flex布局以后, ...