MapKit/CoreLocation使用(二)
来源:互联网 发布:cctv下载网络电视下载 编辑:程序博客网 时间:2024/05/17 01:26
在上文中遵守CLLocationManagerDelegate协议方法 我们可以从其locations获取一下信息
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation* location=[locations lastObject]; NSLog(@"%f %f",location.coordinate.latitude,location.coordinate.longitude); /** location.coordinate; 坐标, 包含经纬度 location.altitude; 设备海拔高度 单位是米 location.course; 设置前进方向 0表示北 90东 180南 270西 location.horizontalAccuracy; 水平精准度 location.verticalAccuracy; 垂直精准度 location.timestamp; 定位信息返回的时间 location.speed; 设备移动速度 单位是米/秒, 适用于行车速度而不太适用于不行 */}
下面说下指南针的使用与实现
定义一个UIImageView对象
@property (strong ,nonatomic)UIImageView* imageView;
设置一个图片
self.imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.width)];
[self.imageViewsetImage:[UIImageimageNamed:@"zhinan.jpg"]];
self.imageView.center=CGPointMake(self.view.center.x,self.view.center.y);
[self.view addSubview:self.imageView];
实现指南针旋转功能
//当获取到用户方向就会调用,CLLocationManagerDelegate协议方法
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
/**
magneticHeading 设备与磁北的相对角度
trueHeading 设置与真北的相对角度,必须和定位一起使用, iOS需要设置的位置来计算真北
真北始终指向地理北极点
磁北对应随着时间变化的地球磁场北极*/
//将获取的角度转换为弧度
CGFloat angle=newHeading.magneticHeading *M_PI / 180;
//旋转图片
self.imageView.transform=CGAffineTransformIdentity;
self.imageView.transform=CGAffineTransformMakeRotation(-angle);
}
效果:旋转手机,手机上的图片会随之旋转,且一直指向北方。(旋转功能需要使用真机测试,模拟器无法模拟)
接下来是对区域的监听
@interfaceViewController ()<CLLocationManagerDelegate>
@property (strong ,nonatomic)CLLocationManager *manager;
@end
- (void)viewDidLoad {
[superviewDidLoad];
self.manager.delegate =self;
//如果为iOS8,想进行区域监测,必须自己请求获取用户隐私的权限(还需在Info.plist文件中添加一项属性,详细操作请见MapKit/CoreLocation使用(一))
if ([[UIDevicecurrentDevice].systemVersiondoubleValue] >= 8.0) {
[self.managerrequestAlwaysAuthorization];
}
//开始检测用户所在区域
/**
创建区域
CLRegion 有两个子类是专门用于指定区域的:
一个专门指定蓝牙范围/一个指定圆形的范围*/
// 创建中心点,东西经各180度,南北纬各90度。
CLLocationCoordinate2D center=CLLocationCoordinate2DMake(39.91405,116.399669);
//创建圆形区域,指定中心点的经纬度以及半径
CLCircularRegion *circular = [[CLCircularRegionalloc] initWithCenter:centerradius:500 identifier:@"天安门"];
[self.managerstartMonitoringForRegion:circular];
}
#pragma mark - CLLocationManagerDelegate
//进入监听区域时调用
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
NSLog(@"进入监听区域");
}
//离开监听区域时调用
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
NSLog(@"离开监听区域");
}
#pragma mark - 懒加载
- (CLLocationManager *)manager{
if (!_manager) {
_manager=[[CLLocationManageralloc] init];
}
return_manager;
}
@end
运行的结果如下图:首先我们使用模拟器将模拟器位置修改为监听区域范围内(如果不知道怎么打开模拟器位置,请见MapKit/CoreLocation使用(一))
之后我们再将模拟器位置修改为监听区域外,经纬度没增加1大概相当于111km。
CLGeocoder
CLGeocoder可以完成“地理编码”以及“反地理编码”。
地理编码:根据给定的定名,获得其具体的位置。例如经纬度,地址的全称等。
反地理编码: 就是根据给定的经纬度,获取具体的位置信息。
地理编码的实现
@interface ViewController ()
//创建地理编码对象
@property (nonatomic ,strong)CLGeocoder *geoder;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//利用地理编码对象编码
//根据传入的地址获取该地址对应的经纬度信息
[self.geodergeocodeAddressString:@"北京" completionHandler:^(NSArray *placemarks,NSError *error) {
//placemarks地标数组,存放着地标,每一个地标包含了该位置的经纬度以及城市/区域/国家代码/邮编等
CLPlacemark *placeMark=[placemarks firstObject];
NSLog(@"%@",placeMark);
}];
}
- (CLGeocoder *)geoder{
if (_geoder ==nil) {
_geoder = [[CLGeocoderalloc] init];
}
return_geoder;
}
@interface ViewController ()
//创建地理编码对象
@property (nonatomic ,strong)CLGeocoder *geoder;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
CLLocation *location = [[CLLocationalloc] initWithLatitude:39.91405longitude:116.399669];
[self.geoderreverseGeocodeLocation:location completionHandler:^(NSArray *placemarks,NSError *error) {
for (CLPlacemark *placeMarkin placemarks) {
NSLog(@"%@",placeMark);
}
}];
}
- (CLGeocoder *)geoder{
if (_geoder ==nil) {
_geoder = [[CLGeocoderalloc] init];
}
return_geoder;
}
github网址:https://github.com/intuit/LocationManager
注意:iOS8需要在Info.plist文件中添加NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescript这两个键值
#import "ViewController.h"
#import "INTULocationManager.h"
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
INTULocationManager *manager= [INTULocationManagersharedInstance];
//获取当前的位置
[manager requestLocationWithDesiredAccuracy:INTULocationAccuracyRoomtimeout:10block:^(CLLocation *currentLocation,INTULocationAccuracy achievedAccuracy,INTULocationStatus status) {
if (status ==INTULocationStatusSuccess) {
NSLog(@"获取位置成功");
}else{
NSLog(@"获取位置失败");
}
}];
//持续获取位置
[managersubscribeToLocationUpdatesWithBlock:^(CLLocation *currentLocation,INTULocationAccuracy achievedAccuracy, INTULocationStatus status) {
NSLog(@"%f %f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
}];
}
@end
- MapKit/CoreLocation使用(二)
- MapKit/CoreLocation使用(一)
- iOS中 CoreLocation 和 MapKit 的使用
- iOS MapKit+CoreLocation
- iOS CoreLocation和MapKit详解
- iOS菜鸟-使用MapKit和CoreLocation实现简单的导航画线
- MapKit&CoreLocation基本使用以及地图大头针的添加与个性化
- iPhone开发之地图定位(CoreLocation和Mapkit框架)简易编程
- iOS- 用MapKit和CoreLocation 来实现移动设备(地图与定位)
- CoreLocation、MapKit和地理围栏1
- MAPKit使用
- CoreLocation使用
- (七十九)MapKit的基本使用
- 使用CoreLocation进行定位(Swift版)
- MapKit的学习二
- MapKit简单使用
- mapKit的使用
- MapKit的基本使用
- 顺序表求集合交并——C++实现
- C#设计没有边框的form
- 如何设计一个app
- centos 6.8 的libmcrypt问题
- 铁路图详解
- MapKit/CoreLocation使用(二)
- spring junit
- 卢卡斯定理
- 一般筛法求素数+快速线性筛法求素数
- 站在巨人肩膀上的人
- 约瑟夫环问题(顺序表)——C++实现
- leftBarButtonItem与backBarButtonItem的区别
- MySQL 5.5的修改字符集编码为UTF8(彻底解决中文乱码问题)
- ESD我的整理