Corelocation及地图控件学习笔记
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及地图控件学习笔记的更多相关文章
- 转)delphi chrome cef3 控件学习笔记 (二)
(转)delphi chrome cef3 控件学习笔记 (二) https://blog.csdn.net/risesoft2012/article/details/51260832 原创 2016 ...
- Winform控件学习笔记【第二天】——常用控件
背景:期末考试刚过就感冒了,嗓子火辣辣的,好难受.但是一想起要学习总结就打起精神来了,Winform控件网上也没有多少使用教程,大部分都是自己在网上零零散散的学的,大部分用的熟了,不总结会很容易忘得. ...
- WinForm控件学习笔记【第一天】——Control类
感悟:明天就又是学校双选会的日子了.两年我都参与了学校的双选会的服务工作,现在该是双选会服务的我时候了.怎么样找到一份好的工作,或者说怎么样学习才能符合企业对人才的要求,我现在也是很迷茫.平时都是在看 ...
- dev控件学习笔记之----CxGrid
本人总结的DEV学习:希望对大家有所帮助. 一.是否显示分组工具: 二.表格左边记录信息显示的宽度: 三.设置表格行高: 四.表头文件的水平和垂直设置:多个设置用按住SHIFT后进行多选,然后就可以设 ...
- web前端开发控件学习笔记之jqgrid+ztree+echarts
版权声明:本文为博主原创文章,转载请注明出处. 作为web前端初学者,今天要记录的是三个控件的使用心得,分别是表格控件jqgrid,树形控件ztree,图表控件echarts.下边分别进行描述. ...
- DataGridView控件-学习笔记总结
1.GridColor属性用来获取或设置网格线的颜色 dataGridView1.GridColor=Color.Blue; 2.设置宽度 .高度 dataGridView1.Columns[].Wi ...
- C# WinForm调用UnityWebPlayer Control控件 <学习笔记1>
工具 1.三维场景 Unity 5.0.2f1 2.开发环境Microsoft Visual Studio 2010 3.需要使用的控件 UnityWebPlayer Control 出现的问题及解决 ...
- Winform控件学习笔记【第六天】——TreeView
TreeView控件用来显示信息的分级视图,如同Windows里的资源管理器的目录.TreeView控件中的各项信息都有一个与之相关的Node对象.TreeView显示Node对象的分层目录结构,每个 ...
- Winform控件学习笔记【第五天】——ListView
[第五天] 常用的基本属性: FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. GridLines:设置行和列之间是否显示网格线.( ...
随机推荐
- HDU 1827:Summer Holiday(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=1827 思路:强连通分量缩点后找入度为0的点,然后对于属于该强连通分量的找一个最小耗费的入口. #include ...
- 跟我一起Django - 01 安装和初始化
跟我一起Django - 01 安装和初始化 python环境 C:\Python27\Lib\site-packages>python --versionPython 2.7.5 安装setu ...
- CSS select样式列表-------美化列表
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- StringBuild,StringFormat," "+" " 三种方法速度测试
测试方式: Stopwatch sw = new Stopwatch(); sw.Start(); string tmp = ""; StringBuilder sb = new ...
- python INFO: Can't locate Tcl/Tk libs and/or headers
安装opencv的时候遇到这个错误: python INFO: Can't locate Tcl/Tk libs and/or headers 参考如下文章解决这个问题: http://www.ver ...
- PowerShell调用jira rest api实现jira统计自动化
通过调用JIRA Rest web api实现统计自动化,首先进行登录模拟: $content = @{username='用户名';password='密码'} $JSON=$content|con ...
- python-dev无法安装
安装tensorflow之前需要先安装python-dev, 这个不能用pip install来安装,只能用sudo apt-get install来安装.安装后会发现还要先装另外两个东西,但是装这两 ...
- HDU 4910 Problem about GCD 找规律+大素数判断+分解因子
Problem about GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- promise理解
每个操作都返回一样的promise对象,保证链式操作 每个链式都通过then方法 每个操作内部允许犯错,出了错误,统一由catch error处理 操作内部,也可以是一个操作链,通过reject或re ...
- datagrid后台分页js.js
$(function () { gridbind(); bindData(); }); //表格绑定function gridbind() { $('#dg').datagrid({ title: ' ...