地图定位

2017-10-23  本文已影响7人  被风吹乱的思念

这个功能主要实现的实时定位

1.注意点 info.plist添加两个文件

NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
屏幕快照 2017-10-23 下午8.04.39.png

2.实现代码

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>{
    
    CLLocationManager *_locationManager;
}


@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 方法一:判断iOS版本号
    if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
        
        // 前台定位授权 官方文档中说明info.plist中必须有NSLocationWhenInUseUsageDescription键
        [_locationManager requestWhenInUseAuthorization];
        // 前后台定位授权 官方文档中说明info.plist中必须有NSLocationAlwaysUsageDescription键
        [_locationManager requestAlwaysAuthorization];
    }
    _locationManager = [[CLLocationManager alloc]init];
    //创建CLLocationManager对象
    _locationManager.delegate=self;
    //设置代理,这样函数didUpdateLocations才会被回调
    [_locationManager requestAlwaysAuthorization];
    ///////  新增的请求定位服务的语句
    [_locationManager startUpdatingLocation];
    //启动定位服务
}
#pragma mark - CoreLocation 代理
#pragma mark 跟踪定位代理方法,每次位置发生变化即会执行(只要定位到相应位置)
//可以通过模拟器设置一个虚拟位置,否则在模拟器中无法调用此方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    CLLocation *location=[locations firstObject];//取出第一个位置
    CLLocationCoordinate2D coordinate=location.coordinate;//位置坐标
    NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);
    //如果不需要实时定位,使用完即使关闭定位服务
    [_locationManager stopUpdatingLocation];
    
    //    CLLocation *location = [locations lastObject];    //当前位置信息
    //
    //    NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f", location.coordinate.longitude, location.coordinate.latitude,location.altitude,location.course,location.speed);
    
    
    
}
// 代理方法中监听授权的改变,被拒绝有两种情况,一是真正被拒绝,二是服务关闭了
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
        {
            NSLog(@"用户未决定");
            break;
        }
            // 系统预留字段,暂时还没用到
        case kCLAuthorizationStatusRestricted:
        {
            NSLog(@"受限制");
            break;
        }
        case kCLAuthorizationStatusDenied:
        {
            // 被拒绝有两种情况 1.设备不支持定位服务 2.定位服务被关闭
            if ([CLLocationManager locationServicesEnabled]) {
                NSLog(@"真正被拒绝");
                // 跳转到设置界面
                NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                if ([[UIApplication sharedApplication] canOpenURL:url]) {
                    
                    [[UIApplication sharedApplication] openURL:url];
                }
            }
            else {
                NSLog(@"没有开启此功能");
            }
            break;
        }
        case kCLAuthorizationStatusAuthorizedAlways:
        {
            NSLog(@"前后台定位授权");
            break;
        }
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        {
            NSLog(@"前台定位授权");
            break;
        }
            
        default:
            break;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
上一篇 下一篇

猜你喜欢

热点阅读