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:设置行和列之间是否显示网格线.( ...
随机推荐
- nginx完美支持tp框架
nginx完美支持tp框架 server { listen 80; server_name mit.520m.com.cn; access_log /data/wwwlogs/mit.520m.com ...
- 修改ECSHOP系统红包序列号规律
ECSHOP系统线下发放红包时系统生成的红包序列号是在10000的基础上增加四位随机数字.如果当我们要发放大额度红包的时候,这样的序列号规则难免给人不安全的感觉,万一真的有哪个无聊的人,用一天时间来蒙 ...
- 5.toogleButton以及Switch
Switch现在用得更多,比toogleButton界面美观,用户体验也要好. 点击事件: 用isChecked来判断点击的是哪一个,然后做相对应的回应.
- sql截断日志
--收缩数据库 DBCC SHRINKDATABASE(fas) --截断事务日志: BACKUP LOG fas WITH NO_LOG 1.清空日志 DUMP TRANSACTION 库名 WIT ...
- [转]android使用shape stroke描边只保留底部
在项目中遇到这种情况:由于一些原因,自己需要用LinearLayout的垂直布局做出ListView的那种效果,但是ListView是自带了分割线的,而且顶部底部都是没有分割线的,每个item中间都是 ...
- c# UDP通信 列子
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 【20160924】GOCVHelper MFC增强算法(2)
//创建或续写目录下的csv文件,填写“文件位置-分类”对 int writeCsv(const string& filename,const Vector<pair<st ...
- php使用 set_include_path
通过set_include_path引用home/lib/image.func.php 1.创建include.php 2.添加如下代码 3.在需要引用的文件中包含include.php文件 < ...
- Intent官方教程(1)简介和作用
Intents An Intent is a messaging object you can use to request an action from another app component. ...
- 【项目管理和构建】十分钟教程,eclipse配置maven + 创建maven项目(三)
[项目管理和构建]十分钟教程,eclipse配置maven + 创建maven项目(三) 上篇博文中我们介绍了maven下载.安装和配置(二),这篇博文我们配置一下eclipse,将它和maven结合 ...