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路由器的地理位置的信息.比较省电, ...
随机推荐
- SqlSugar ORM框架文档
http://www.codeisbug.com/Doc/8/1141 SqlSugar入门级教程+实例 (.net core下的)https://www.cnblogs.com/rulasann/p ...
- vue 修饰符(转载)
大佬写的很详细,直接转载过来,随时可以参考, 原博:https://www.w3cplus.com/vue/vue-methods-and-event-handling.html 事件处理 如果需要在 ...
- oracle 控制文件损坏处理
一, 故障模拟 控制文件损坏 发现关闭不了 强制关闭 故障恢复 发现已经执行到mont阶段,因为这个不依靠控制文件 进入整段日志 cd /u01/app/oracle/diag/rdbms/o ...
- HTML练习 | 百度搜索框
<!DOCTYPE html> <head> <title>百度首页</title> <style> .logo{ background:u ...
- 使用bind配置DNS服务(CentOS 6.5)
DNS域名解析服务(Domain Name System)是用于解析域名与IP地址对应关系的服务,功能上可以实现正向解析与反向解析: 正向解析:根据主机名(域名)查找对应的IP地址. 反向解析:根据I ...
- JDBC的PreparedStatement启动事务使用批处理executeBatch()
JDBC使用MySQL处理大数据的时候,自然而然的想到要使用批处理, 普通的执行过程是:每处理一条数据,就访问一次数据库: 而批处理是:累积到一定数量,再一次性提交到数据库,减少了与数据库的交互次数, ...
- 记一次坑爹的jconsole使用
之前一直是用jstat来监控GC的,后来发现原来有个自带的jconsole,是可始化界面的,而且也是oracle公司自带的工具,与是拿来用一下,发现蛮好用的. 然而,在一次复现实验中,发现原来能复现的 ...
- 自定义django admin及其界面
1.在项目目录下新创建一个app,命名为kingadmin,在templates目录下新建kingadmin目录,用来存放相关页面的模板文件,新建一个templatetags目录,用来存放处理前端模板 ...
- jqgrid 操作
1.获取单个id 获取行号,有这种方式: var rowid = $("#gridList").jqGrid("getGridParam", "sel ...
- mysql 查两个表相同的值
比如一个数据库 表A和表B 都有一个username字段, 现查出与表A中username值相同的表B的username和password数据 select B.username,B.password ...