定位-01-定位服务编程

//
//  ViewController.m
//  MyLocation
//
//  Created by JohnFighting on 16/11/2015.
//  Copyright © 2015 john. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>
//经度
@property (weak, nonatomic) IBOutlet UITextField *txtLng;
//纬度
@property (weak, nonatomic) IBOutlet UITextField *txtLat;
//高度
@property (weak, nonatomic) IBOutlet UITextField *txtAlt;

@property (nonatomic, strong) CLLocationManager *locationManager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //定位服务管理对象初始化

    //创建并初始化locationManager属性
    self.locationManager = [[CLLocationManager alloc]init];
    //设置定位服务委托对象为self
    self.locationManager.delegate = self;
    //设置desiredAccuracy属性(定位精度)
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //设置distanceFilter属性, 定义了设备移动后获得位置信息的最小距离, 单位是米
    self.locationManager.distanceFilter = 1000.0f;

    //弹出用户授权对话框
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager requestAlwaysAuthorization];
}

//在视图出现时调用
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //开始定位, 在视图控制器的声明周期方法viewWillAppear:中使用这个方法最为合适
    [self.locationManager startUpdatingLocation];
}

//在视图消失时调用
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //停止定位,
    [self.locationManager stopUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    CLLocation *currLocation = [locations lastObject];//获得集合中的最后一个元素, 即为设备的当前位置

    //coordinate封装经度和纬度的结构体
    self.txtLat.text = [NSString stringWithFormat:@"%3.5f",currLocation.coordinate.latitude];//获取设备当前的纬度
    self.txtLng.text = [NSString stringWithFormat:@"%3.5f",currLocation.coordinate.longitude];//获取设备当前的经度
    self.txtAlt.text = [NSString stringWithFormat:@"%3.5f",currLocation.altitude];//获取设备当前的高度
}

//定位失败时调用
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"error: %@",error);
}

//授权状态发生变化时调用
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{//status是获得的授权信息, 也可以使用self.locationManager.authorizationStatus = kCLAuthorizationStatusNotDetermined;获得授权信息
    if (status == kCLAuthorizationStatusAuthorizedAlways) {
        NSLog(@"Authorized");
    }else if (status == kCLAuthorizationStatusAuthorizedWhenInUse){
        NSLog(@"AuthorizedWhenInUse");
    }else if (status == kCLAuthorizationStatusDenied){
        NSLog(@"Denied");
    }else if (status == kCLAuthorizationStatusRestricted){
        NSLog(@"Restricted");
    }else if (status == kCLAuthorizationStatusNotDetermined){
        NSLog(@"Determined");
    }
}

@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值