热门搜索 :
考研考公
您的当前位置:首页正文

地图定位功能的使用

来源:东饰资讯网

定位功能

1.先导入地图的库
// 定位服务
#import <CoreLocation/CoreLocation.h>

2.创建定位管理对象
@property (nonatomic, strong) CLLocationManager * locationManager; 

3.设置代理
@interface AppDetailViewController ()<CLLocationManagerDelegate>
  
4.协议内容
// 授权状态改变时的回调,第一次进入App或者第一次请求用户权限
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    // 当授权完成时,启动定位服务,获取定位信息
    if (status >= kCLAuthorizationStatusAuthorizedAlways) {
        [manager startUpdatingLocation];
    }
    else if (status == kCLAuthorizationStatusDenied)
    {
        // 不允许定位服务,创建警示窗,对用户进行提示
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"您已关闭定位,App的部分功能可能无法使用,请在设置中开启,么么哒" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
    }
}
5.在info.plist中添加NSLocationWhenInUseUsageDescription

在后输入用户提示语


效果图
6. 配置定位请求
- (void)configLocationManger
{
    self.locationManager = [[CLLocationManager alloc] init];
    // 精确度
    self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    // 刷新频率 每过100米刷新一次
    self.locationManager.distanceFilter = 100.0;
    // 设置委托
    self.locationManager.delegate = self;
    
    // 请求用户权限
    // 判断当前系统版本 8.0之前版本不需要用户授权位置定位
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        // 请求WhenInUse权限
        [self.locationManager requestWhenInUseAuthorization];
        // 在info.plist文件中添加NSLocationWhenInUseUsageDescription
    }
    // 启动定位服务
    [self.locationManager startUpdatingLocation];
}

7.地球坐标转火星坐标
导入库


转坐标封装的方法
//协议方法: 当定位服务返回定位信息时的回调
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    if (locations.count > 0) {
        // 停止定位服务
        [manager stopUpdatingLocation];
        
        CLLocation * location = [locations lastObject];
        // 将地球坐标转换为火星坐标,返回火星坐标
        CLLocation * marsLocation = [location locationMarsFromEarth];
        //获取经度
        marsLocation.coordinate.longitude;
        //获取纬度
        marsLocation.coordinate.latitude;
       
    }
}

Top