定位功能(使用系统地图)

1.导入MapKit,CoreLocation库

2.viewController文件

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

@interface ViewController ()<CLLocationManagerDelegate>
{
    CLLocationManager *_locationManager;
}
@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    //1.创建locationManager
    _locationManager = [[CLLocationManager alloc] init];
    
    //2.info.plist文件加入字段
    //NSLocationWhenInUseDescription
    //NSLocationAlwaysUsageDescription
    
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"定位服务未打开");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请打开定位服务" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    
    //3.请求权限(一直允许访问位置信息)
    [_locationManager requestAlwaysAuthorization];
    //程序运行期间允许访问位置信息
    //[_locationManager requestWhenInUseAuthorization];
    
    //4.定位精确度
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //设置隔多少米后更新位置信息
    _locationManager.distanceFilter = 100;
    _locationManager.delegate = self;
    
    //5.开启定位
    [_locationManager startUpdatingLocation];
}



#pragma mark --CLLocationManagerDelegate---
//权限状态改变
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    NSLog(@"%i",status);
    //给出相应提示信息
    if (status != kCLAuthorizationStatusAuthorizedAlways) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请设置程序允许访问位置信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    else
    {
        [_locationManager startUpdatingLocation];
    }
}
//更新到用户位置信息
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations firstObject];
    NSLog(@"%f-%f",location.coordinate.latitude,location.coordinate.longitude);
    //反编码
    CLGeocoder *geo = [[CLGeocoder alloc] init];
    [geo reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"%@",error);
    }];
}

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

@end

 

转载于:https://www.cnblogs.com/liaods/p/4805378.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值