IOS CoreLocation框架的使用(用于地理定位)
● 导航:去任意陌生的地方
● 周边:找餐馆、找酒店、找银行、找电影院
● 在上述应用中,都用到了地图和定位功能,在iOS开发中,要想加入这2 大功 能,必须基于2个框架进行开发
● Map Kit :用于地图展示
● Core Location :用于地理定位
● 2个热门专业术语
● LBS :Location Based Service
● SoLoMo :Social Local Mobile(索罗门)
CoreLocation框架的使用
#import <CoreLocation/CoreLocation.h>
● CoreLocation框架使用须知
● CoreLocation框架中所有数据类型的前缀都是CL
● CoreLocation中使用CLLocationManager对象来做用户定位
CLLocationManager
● 开始用户定位
• - (void)startUpdatingLocation;
● 停止用户定位
• - (void) stopUpdatingLocation;
● 当调用了startUpdatingLocation方法后,就开始不断地定位用户的位
置,中途会频繁地调用代理的下面方法
● - (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations;
• locations参数里面装着CLLocation对象
CLLocation
● @property(readonly,nonatomic)CLLocationCoordinate2Dcoordinate;
• 经纬度
● @property(readonly,nonatomic)CLLocationDistancealtitude;
• 海拔
● @property(readonly,nonatomic)CLLocationDirectioncourse;
• 路线,航向(取值范围是0.0°~359.9°,0.0°代表真北方向)
● @property(readonly,nonatomic)CLLocationSpeedspeed;
• 行走速度(单位是m/s)
● 用- (CLLocationDistance)distanceFromLocation:(const CLLocation*)location
方法可以计算2个位置之间的距离
CLLocationManager
● 每隔多少米定位一次
● @property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;
● 定位精确度(越精确就越耗电)
CLLocationCoordinate2D
CLLocationDegrees latitude; // 纬度
CLLocationDegrees longitude; // 经度
}CLLocationCoordinate2D;
● 一般用CLLocationCoordinate2DMake函数来创建CLLocationCoordinate2D
用户隐私的保护
● 从iOS 6开始,苹果在保护用户隐私方面做了很大的加强,以下操作都必须经 过用户批准授权
● 要想获得用户的位置
● 想访问用户的通讯录、日历、相机、相册等等
● 当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权
● 为了严谨起见,最好在使用定位功能之前判断当前应用的定位功能是否可用
• + (BOOL)locationServicesEnabled;
CLGeocoder
● 地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
● 反地理编码:根据给定的经纬度,获得具体的位置信息
● 地理编码方法
● -(void)geocodeAddressString:(NSString*)addressString
completionHandler:
(CLGeocodeCompletionHandler)completionHandler;
● 反地理编码方法
● -(void)reverseGeocodeLocation:(CLLocation*)location
completionHandler:
(CLGeocodeCompletionHandler)completionHandler;
CLGeocodeCompletionHandler
● 这个block传递2个参数
• error :当编码出错时(比如编码不出具体的信息)有值
• placemarks :里面装着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;
• 城市
实例:Core Location :用于地理定位
#import "HMViewController.h"
#import <CoreLocation/CoreLocation.h> @interface HMViewController () <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locMgr;
@end @implementation HMViewController - (CLLocationManager *)locMgr
{
if (!_locMgr) {
// 1.创建位置管理器(定位用户的位置)
self.locMgr = [[CLLocationManager alloc] init]; // 2.设置代理
self.locMgr.delegate = self;
}
return _locMgr;
} - (void)viewDidLoad
{
[super viewDidLoad]; if ([CLLocationManager locationServicesEnabled]) {
// 开始定位用户的位置
[self.locMgr startUpdatingLocation]; // [self.locMgr startMonitoringForRegion:<#(CLRegion *)#>]; // self.locMgr.purpose // self.locMgr.distanceFilter = kCLDistanceFilterNone;
// self.locMgr.desiredAccuracy = kcllocationac
} else { // 不能定位用户的位置
// 1.告诉用户检查网络状况
// 2.提醒用户打开定位开关
} [self countDistance];
} - (void)countDistance
{
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude: longitude:];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude: longitude:]; CLLocationDistance distance = [loc1 distanceFromLocation:loc2];
NSLog(@"(%@)和(%@)的距离:%f", loc1, loc2, distance);
} #pragma mark - CLLocationManagerDelegate
/**
* 当定位到用户的位置时,就会调用(调用比较频繁)
*/
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// 数组里面存放的是CLLocation对象, 一个CLLocation就代表一个位置
CLLocation *loc = [locations lastObject]; // 纬度:loc.coordinate.latitude
// 经度:loc.coordinate.longitude
NSLog(@"纬度=%f, 经度=%f", loc.coordinate.latitude, loc.coordinate.longitude); // 停止更新位置(不用定位服务,应当马上停止定位,非常耗电)
[manager stopUpdatingLocation];
} //- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
//{
//
//}
//
//- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
//{
//
//} @end
实例:地理编码 and 反地理编码
#import "HMViewController.h"
#import <CoreLocation/CoreLocation.h> @interface HMViewController ()
@property (nonatomic, strong) CLGeocoder *geocoder; #pragma mark - 地理编码
- (IBAction)geocode;
@property (weak, nonatomic) IBOutlet UITextField *addressField;
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel; #pragma mark - 反地理编码
- (IBAction)reverseGeocode;
@property (weak, nonatomic) IBOutlet UITextField *longtitudeField;
@property (weak, nonatomic) IBOutlet UITextField *latitudeField;
@property (weak, nonatomic) IBOutlet UILabel *reverseDetailAddressLabel;
@end @implementation HMViewController - (CLGeocoder *)geocoder
{
if (!_geocoder) {
self.geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
} - (void)viewDidLoad
{
[super viewDidLoad]; } /**
* 地理编码:地名 -> 经纬度
*/
- (void)geocode
{
// 1.获得输入的地址
NSString *address = self.addressField.text;
if (address.length == ) return; // 2.开始编码
[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
if (error || placemarks.count == ) {
self.detailAddressLabel.text = @"你输入的地址找不到,可能在火星上";
} else { // 编码成功(找到了具体的位置信息)
// 输出查询到的所有地标信息
for (CLPlacemark *placemark in placemarks) {
NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@", placemark.name, placemark.locality, placemark.country, placemark.postalCode);
} // 显示最前面的地标信息
CLPlacemark *firstPlacemark = [placemarks firstObject];
self.detailAddressLabel.text = firstPlacemark.name; CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude;
CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude;
self.latitudeLabel.text = [NSString stringWithFormat:@"%.2f", latitude];
self.longitudeLabel.text = [NSString stringWithFormat:@"%.2f", longitude];
}
}];
} /**
* 反地理编码:经纬度 -> 地名
*/
- (void)reverseGeocode
{
NSString *longtitudeText = self.longtitudeField.text;
NSString *latitudeText = self.latitudeField.text;
if (longtitudeText.length == || latitudeText.length == ) return; CLLocationDegrees latitude = [latitudeText doubleValue];
CLLocationDegrees longtitude = [longtitudeText doubleValue]; // 开始反向编码
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longtitude];
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if (error || placemarks.count == ) {
self.reverseDetailAddressLabel.text = @"你输入的经纬度找不到,可能在火星上";
} else { // 编码成功(找到了具体的位置信息)
// 输出查询到的所有地标信息
for (CLPlacemark *placemark in placemarks) {
NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@", placemark.name, placemark.locality, placemark.country, placemark.postalCode);
} // 显示最前面的地标信息
CLPlacemark *firstPlacemark = [placemarks firstObject];
self.reverseDetailAddressLabel.text = firstPlacemark.name; CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude;
CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude;
self.latitudeField.text = [NSString stringWithFormat:@"%.2f", latitude];
self.longtitudeField.text = [NSString stringWithFormat:@"%.2f", longitude];
}
}];
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
} @end
IOS CoreLocation框架的使用(用于地理定位)的更多相关文章
- Core Location :⽤用于地理定位
Core Location :⽤用于地理定位 在移动互联⽹网时代,移动app能解决⽤用户的很多⽣生活琐事,⽐比如 导航:去任意陌⽣生的地⽅方 周边:找餐馆.找酒店.找银⾏行.找电影院 在上述应⽤用中, ...
- iOS开发——高级篇——地理定位 CoreLocation
一.CoreLocation 在移动互联网时代,移动app能解决用户的很多生活琐事,比如周边:找餐馆.找KTV.找电影院等等导航:根据用户设定的起点和终点,进行路线规划,并指引用户如何到达 在上述应用 ...
- CoreLocation框架的使用---定位,求两地距离
前言: 在iOS开发中,有关导航,周边的开发,必须基于2个框架: Map Kit :用于地图展示 Core Location :用于地理定位 用户隐私的保护 从iOS 6开始,苹果在保护用户隐私方 ...
- iOS UI进阶-4.0 地图与定位
在移动互联网时代,移动app能解决用户的很多生活琐事,比如 导航:去任意陌生的地方 周边:找餐馆.找酒店.找银行.找电影院 在上述应用中,都用到了地图和定位功能,在iOS开发中,要想加入这2大功能 ...
- CoreLocation框架的使用
CoreLocation框架使用 一.地图和定位的简介 1.应用场景 周边:找餐馆/找KTV/找电影院(团购APP) 导航:根据用户设定的起点和终点,进行路线规划,并指引用户如何到达(地图APP) 2 ...
- 【iOS】7.4 定位服务->2.1.3.1 定位 - 官方框架CoreLocation 功能1:地理定位
本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...
- iOS定位--CoreLocation框架
CoreLocation框架的使用 // 首先导入头文件 #import <CoreLocation/CoreLocation.h> CoreLocation框架中所有数据类型的前缀都是C ...
- 地图定位CoreLocation框架,地理位置编码与反编码
在现代互联网时代,越来越多的应用,都用到了地图定位功能,在iOS开发中,想要加入这种功能,必须基于两个框架进行开发: 1.Map Kit:用于显示地图, 2.CoreLocation:用于显示地理位置 ...
- ios开发——实用技术OC篇&地图与定位
地图与定位 11.1 iOS定位服务 11.2 iOS地图 11.3 Web地图 1 iOS定位服务 iOS中有三个定位服务组件: Wifi定位,通过查询一个Wifi路由器的地理位置的信息.比较省电, ...
随机推荐
- Python+Selenium之HTMLTestRunner
下载 HTMLTestRunner 模块 下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html 保存路径:将下载的HTMLTestRunne ...
- GreenPlum 大数据平台--远程访问-->gpadmin客户端
一,客户端连接 01,配置文件说明 在master节点的$MASTER_DATA_DIRECTORY(这个是配置的环境变量:/greenplum/data/master/gpseg-1)/pg_hba ...
- Docker的安装和镜像管理并利用Docker容器实现nginx的负载均衡、动静分离
Docker的安装 一.Docker的概念 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化 ...
- 客户端与服务器cookie
认识cookie 第一部分: 概要 cookie是一种早期使用的客户端存储机制(现在仍在广泛使用),cookie数据会在Web浏览器和Web服务器之间传输, 因为早期cookie是针对服务器脚本设计的 ...
- js判断文件是否存在的方法
在做电力监控项目的时候,有一个需求就是左右布局的框架,点击左边的图形文件地址,然后去文件夹中找到文件,再在右边出现对应的图形文件,但是有些文件可能是配置的时候有问题,找不到文件,所以js需要判断,以下 ...
- Best HTTP
http://blog.csdn.net/u012322710/article/details/52860747 Best HTTP (Pro) 这是一款很多公司都在用的网页插件,感觉确实不错,分P ...
- 触发Full GC的时机
由于Full GC的耗时是Minor GC的十倍左右,所以Full GC的频率设计得比Minor GC低得多.现总结一下触发Full GC的情况. 在那些实现了CMS的比较新的虚拟机中,如果配置了-X ...
- 《腾讯游戏人生》微信小程序开发总结
为打通游戏人生擂台赛与线下商家的O2O衔接,同时响应时下日臻火热的微信小程序,项目团队决定也开发一款针对性的微信小程序,以此方便商家在我们平台入驻并进行擂台赛事的创建和奖励的核销,进一步推广擂台赛的玩 ...
- flask-restful 请求解析
基本参数 from flask import Flask from flask.ext.restful import reqparse, abort, Api, Resource app = Flas ...
- C++程序设计基础(5)sizeof的使用
1.知识点 (1)sizeof是一个单目运算发,而不是一个函数,其用于获取操作数所占内存空间的字节数. (2)sizeof的操作数可以使类型名,也可以是表达式,如果是类型名则直接获得该类型所占字节数, ...