Corelocation基本使用

在地图章节的学习中,首先要学的便是用户位置定位,因此我们首先要掌握Corelocation的使用。(在IOS8以前可以系统会直接请求授权,现在需要我们自己调用方式通知系统请求授权)

首先设置一个Corelocation属性并实现懒加载设置代理,此对象需要自己调用方法startUpdatingLocation及stopUpdatingLocation来开始和结束位置获取

 //定位管理者
@property (nonatomic , strong ) CLLocationManager *manager;
- (CLLocationManager *)manager{
if (_manager == nil) {
//创建CoreLocation管理者
_manager = [[CLLocationManager alloc] init];
7 _manager.delegate = self;
8 }
9 return _manager;
10 }

设置好定位管理者对象后就对其属性进行一些常用的属性配置

 //配置定位精度
/*
导航级别: kCLLocationAccuracyBestForNavigation;
最优: kCLLocationAccuracyBest;
精确到10米: kCLLocationAccuracyNearestTenMeters;
精确到100米: kCLLocationAccuracyHundredMeters;
精确到1000米: kCLLocationAccuracyKilometer;
精确到3000米: kCLLocationAccuracyThreeKilometers;
*/
self.manager.desiredAccuracy = kCLLocationAccuracyBest;
//设置超过范围后更新数据,如不设置数据会一直不间断更新
self.manager.distanceFilter = ;

若使用[self.manager requestAlwaysAuthorization]方法需要添加NSLocationAlwaysUsageDescription为key

 //判断系统IOS版本
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
NSLog(@"IOS8");
//注意:IOS8后需要验证授权,在Info.plist文件还要加上NSLocationWhenInUseUsageDescription这个key,Value可以为空,并调用此方法
[self.manager requestWhenInUseAuthorization];
[self.manager startUpdatingLocation];
}else{
NSLog(@"IOS7");
//开始获取用户位置
[self.manager startUpdatingLocation];
}

接下来便需要实现一些代理方法来方便我们获取用户位置,以下为常用的部分代理协议

1.用户授权状态改变后调用

 //在此判断授权状态并进行相对应的操作
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if (status == kCLAuthorizationStatusAuthorizedAlways ||
status == kCLAuthorizationStatusAuthorizedWhenInUse) {
NSLog(@"授权成功");
//授权成功后开始监听 获取位置
[self.manager startUpdatingLocation];
}
}

2.在更新用户位置时调用的方法

 //可以在这里进行反地理编码获取位置信息
- (void)locationManager:(CLLocationManager *)managerdidUpdateLocations:(NSArray *)locations{
//反地理编码使用位置信息反编码
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *newLocation = [locations lastObject];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark *place in placemarks) {
//通过CLPlacemark可以输出用户位置信息
NSLog(@"%@ %@ %lf %lf",place.name, place.addressDictionary, place.location.coordinate.latitude,place.location.coordinate.longitude);
}
}];
}

3.用户手机头部方向改变时调用的方法

 //在更新用户头部方向时调用,适用方向指定操作
- (void)locationManager:(CLLocationManager *)managerdidUpdateHeading:(CLHeading *)newHeading

至此Corelocation的配置就已完成,接下来是关于地图配置的方法

地图控件的基本使用

首先要导入地图框架 #import <MapKit/MapKit.h>

设置地图控件属性,并懒加载实现及其属性配置

 //地图控件,其中region属性个人觉得在代理中设置比较合适
@property (nonatomic, strong) MKMapView *mapView;
- (MKMapView *)mapView{
if (_mapView == nil) {
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
_mapView.delegate = self;
/*
MKUserTrackingModeNone = 0 默认不跟踪
MKUserTrackingModeFollow, 追踪位置
MKUserTrackingModeFollowWithHeading 追踪位置及其头部指向
*/
//用户追踪模型,如不设置将无法追踪用户
_mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
/*
MKMapTypeStandard = 0,普通模式(默认模式)
MKMapTypeSatellite, 卫星模式
MKMapTypeHybrid 混合模式
*/
_mapView.mapType = MKMapTypeStandard;
}
return _mapView;
}

最后将_mapView添加进self.view中,整个地图相当于就可以加载完成了。下面介绍MKMapView的一部分代理方法及其调用时机

 #pragma mark - MKMapViewDelegate 代理方法
/**
* 更新用户位置是调用
*
* @param mapView 题图
* @param userLocation 用户位置
*/
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
NSLog(@"更新用户位置");
//设置用户位置中心点
[self.mapView setCenterCoordinate:userLocation.coordinate animated:YES];
//范围设置MKCoordinateSpanMake中的代表经纬度范围
MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.coordinate, MKCoordinateSpanMake(0.1, 0.1));
//设置中心点范围
[self.mapView setRegion:region animated:YES];
}
/**
* 地图将要开始渲染时调用
*/
- (void)mapViewWillStartRenderingMap:(MKMapView *)mapView{
NSLog(@"%s",__func__);
}
/**
* 地图完成渲染时调用
*/
- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered{
NSLog(@"%s",__func__);
}
/**
* region(范围)即将改变时调用
*/
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
NSLog(@"%s",__func__);
}
/**
* region(范围)完成改变时调用
*/
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
NSLog(@"%s",__func__);
}
/**
* 追踪模型改变时调用
*/
- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated{
NSLog(@"%s",__func__);
}
/**
* 设置大头针时调用 类似于tableViewCell设置
*/
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
51 //在其中设置MKAnnotationVIew,有重用机制
52}

另外还有其他一些代理方法就不一一介绍了,剩下的就看需要 在对应的方法里添加想进行的操作。

Corelocation及地图控件学习笔记的更多相关文章

  1. 转)delphi chrome cef3 控件学习笔记 (二)

    (转)delphi chrome cef3 控件学习笔记 (二) https://blog.csdn.net/risesoft2012/article/details/51260832 原创 2016 ...

  2. Winform控件学习笔记【第二天】——常用控件

    背景:期末考试刚过就感冒了,嗓子火辣辣的,好难受.但是一想起要学习总结就打起精神来了,Winform控件网上也没有多少使用教程,大部分都是自己在网上零零散散的学的,大部分用的熟了,不总结会很容易忘得. ...

  3. WinForm控件学习笔记【第一天】——Control类

    感悟:明天就又是学校双选会的日子了.两年我都参与了学校的双选会的服务工作,现在该是双选会服务的我时候了.怎么样找到一份好的工作,或者说怎么样学习才能符合企业对人才的要求,我现在也是很迷茫.平时都是在看 ...

  4. dev控件学习笔记之----CxGrid

    本人总结的DEV学习:希望对大家有所帮助. 一.是否显示分组工具: 二.表格左边记录信息显示的宽度: 三.设置表格行高: 四.表头文件的水平和垂直设置:多个设置用按住SHIFT后进行多选,然后就可以设 ...

  5. web前端开发控件学习笔记之jqgrid+ztree+echarts

    版权声明:本文为博主原创文章,转载请注明出处.   作为web前端初学者,今天要记录的是三个控件的使用心得,分别是表格控件jqgrid,树形控件ztree,图表控件echarts.下边分别进行描述. ...

  6. DataGridView控件-学习笔记总结

    1.GridColor属性用来获取或设置网格线的颜色 dataGridView1.GridColor=Color.Blue; 2.设置宽度 .高度 dataGridView1.Columns[].Wi ...

  7. C# WinForm调用UnityWebPlayer Control控件 <学习笔记1>

    工具 1.三维场景 Unity 5.0.2f1 2.开发环境Microsoft Visual Studio 2010 3.需要使用的控件 UnityWebPlayer Control 出现的问题及解决 ...

  8. Winform控件学习笔记【第六天】——TreeView

    TreeView控件用来显示信息的分级视图,如同Windows里的资源管理器的目录.TreeView控件中的各项信息都有一个与之相关的Node对象.TreeView显示Node对象的分层目录结构,每个 ...

  9. Winform控件学习笔记【第五天】——ListView

    [第五天] 常用的基本属性: FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. GridLines:设置行和列之间是否显示网格线.( ...

随机推荐

  1. ios app的真机调试与发布配置

    1.打开应用程序—>[钥匙串访问]—>[证书助理]—>[从证书办法机构请求证书]     2.在[用户电子邮件地址]填入apple账户用的邮箱,选择[存储到磁盘],点击[继续],会在 ...

  2. sass初步认识3

    sass的混合器是对大段的样式代码进行命名,定义为混合器,以便被多次引用.混合器的格式为:“@mixin 样式名称{样式代码}”:调用混合器的格式为:“@include 样式名称”.例:@minin ...

  3. UWP开发入门系列笔记之(一):UWP初览

    标签: 随着微软Build2015带来的好消息,Win10正式版发布的日子已经离我们越来越近了,我们也终于欣喜地看到:一个统一的Windows平台对于开发人员来说充满了吸引力,这局棋下的好大的说--于 ...

  4. Codeforces Round #336 Zuma

    D. Zuma time limit per test:  2 seconds memory limit per test:  512 megabytes input:  standard input ...

  5. Poj(2253),Dijkstra松弛条件的变形

    题目链接:http://poj.org/problem?id=2253 题意: 给出两只青蛙的坐标A.B,和其他的n-2个坐标,任一两个坐标点间都是双向连通的.显然从A到B存在至少一条的通路,每一条通 ...

  6. Callable, Runnable, Future, FutureTask

    Java并发编程之Callable, Runnable, Future, FutureTask Java中存在Callable, Runnable, Future, FutureTask这几个与线程相 ...

  7. 2016年11月8日 星期二 --出埃及记 Exodus 19:24

    2016年11月8日 星期二 --出埃及记 Exodus 19:24 The LORD replied, "Go down and bring Aaron up with you. But ...

  8. [转]Linq中GroupBy方法的使用总结

    Demo模型类: public class StudentScore { public int ID { set; get; } public string Name { set; get; } pu ...

  9. Linux 性能监测:Network

    网络的监测是所有 Linux 子系统里面最复杂的,有太多的因素在里面,比如:延迟.阻塞.冲突.丢包等,更糟的是与 Linux 主机相连的路由器.交换机.无线信号都会影响到整体网络并且很难判断是因为 L ...

  10. .Net面試題

    初级.NET开发人员 - 任何使用.NET的人都应知道的 1. 描述线程与进程的区别? 进程是系统所有资源分配时候的一个基本单位,拥有一个完整的虚拟空间地址,并不依赖线程而独立存在.进程可以定义程序的 ...