CLLocationManager位置服务对象和地图的简单结合使用
2015-07-04 11:18
405 查看
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 1.创建地图视图对象
_mapView = [[MKMapView
alloc]
initWithFrame:self.view.bounds];
//
设置地图模式
_mapView.mapType =
MKMapTypeStandard;
[self.view
addSubview:_mapView];
// 2.创建位置服务对象
_locationManager = [[CLLocationManager
alloc]
init];
//
设置代理的对象
_locationManager.delegate =
self;
//
设置精准度
[_locationManager
setDesiredAccuracy:kCLLocationAccuracyBest];
//
如需定位服务
/* iOS8.0如需使用位置服务
requestAlwaysAuthorization -->询问是否允许访问你的位置(永久授权)
NSLocationAlwaysUsageDescription -->requestWhenInUseAuthorization(使用中授权)
注意:此处需要在plist文件中配置key
1.永久授权设置 NSLocationAlwaysUsageDescription (配置Value可以设置字符串,会显示在弹出视图中)
2.使用中授权设置 NSLocationWhenInUseUsageDescription (配置Value可以设置字符串,会显示在弹出视图中)
*/
if ([_locationManager
respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
// [_locationManager requestAlwaysAuthorization];
[_locationManager
requestWhenInUseAuthorization];
}
//
开始定位
[_locationManager
startUpdatingLocation];
}
/*
CLLocation用来表示某个位置的地理信息,比如经纬度、海拔等等
(1)经纬度
@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;
(2)海拔
@property(readonly, nonatomic) CLLocationDistance altitude;
(3)路线,航向(取值范围是0.0° ~ 359.9°,0.0°代表真北方向)
@property(readonly, nonatomic) CLLocationDirection course;
(4)行走速度(单位是m/s)
@property(readonly, nonatomic) CLLocationSpeed speed;
(5)计算2个位置之间的距离
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location方法
*/
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray
*)locations
{
// 1.停止定位
[_locationManager
stopUpdatingLocation];
// 2.设置地图现实自身位置
_mapView.showsUserLocation =
YES;
// 3.获取当前手机位置
CLLocation *location = [locations
lastObject];
//
// 4.设置地图显示区域
// 地图缩放比例
MKCoordinateSpan span =
MKCoordinateSpanMake(.1,
.1);
//
地图显示区域
MKCoordinateRegion region =
MKCoordinateRegionMake(location.coordinate, span);
[_mapView
setRegion:region];
}
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 1.创建地图视图对象
_mapView = [[MKMapView
alloc]
initWithFrame:self.view.bounds];
//
设置地图模式
_mapView.mapType =
MKMapTypeStandard;
[self.view
addSubview:_mapView];
// 2.创建位置服务对象
_locationManager = [[CLLocationManager
alloc]
init];
//
设置代理的对象
_locationManager.delegate =
self;
//
设置精准度
[_locationManager
setDesiredAccuracy:kCLLocationAccuracyBest];
//
如需定位服务
/* iOS8.0如需使用位置服务
requestAlwaysAuthorization -->询问是否允许访问你的位置(永久授权)
NSLocationAlwaysUsageDescription -->requestWhenInUseAuthorization(使用中授权)
注意:此处需要在plist文件中配置key
1.永久授权设置 NSLocationAlwaysUsageDescription (配置Value可以设置字符串,会显示在弹出视图中)
2.使用中授权设置 NSLocationWhenInUseUsageDescription (配置Value可以设置字符串,会显示在弹出视图中)
*/
if ([_locationManager
respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
// [_locationManager requestAlwaysAuthorization];
[_locationManager
requestWhenInUseAuthorization];
}
//
开始定位
[_locationManager
startUpdatingLocation];
}
/*
CLLocation用来表示某个位置的地理信息,比如经纬度、海拔等等
(1)经纬度
@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;
(2)海拔
@property(readonly, nonatomic) CLLocationDistance altitude;
(3)路线,航向(取值范围是0.0° ~ 359.9°,0.0°代表真北方向)
@property(readonly, nonatomic) CLLocationDirection course;
(4)行走速度(单位是m/s)
@property(readonly, nonatomic) CLLocationSpeed speed;
(5)计算2个位置之间的距离
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location方法
*/
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray
*)locations
{
// 1.停止定位
[_locationManager
stopUpdatingLocation];
// 2.设置地图现实自身位置
_mapView.showsUserLocation =
YES;
// 3.获取当前手机位置
CLLocation *location = [locations
lastObject];
//
// 4.设置地图显示区域
// 地图缩放比例
MKCoordinateSpan span =
MKCoordinateSpanMake(.1,
.1);
//
地图显示区域
MKCoordinateRegion region =
MKCoordinateRegionMake(location.coordinate, span);
[_mapView
setRegion:region];
}
相关文章推荐
- [转] iOS - LLDB调试命令初探
- centos 6.5 安装mongodb2.6
- Mysql主从配置
- utf-8 BOM
- [thinkphp] 是如何输出一个页面的
- 两个实验操作系统-ubuntu在安装配置pintos
- C语言的指针
- CentOS6.3挂载读写NTFS分区
- RedHat 下的火狐浏览器查看cookie
- 有道词典
- Opencv3.0.0+Visual Studio 2013+win8.1 64位 配置大致思路
- Windows下通过BAT执行文件下载任务
- [BZOJ 3884][欧拉定理]上帝与集合的正确用法
- 能量英语(三) 之 “情感把控 II ”
- c语言指针详解
- jquery学习笔记之二
- 【一天一个canvas】绘制矩形或正方形(三)
- 给定一个0-1串,请找到一个尽可能长的子串,其中包含的0与1的个数相等。
- 杂文 - 优化工作效率
- FTP站点