iOS获取定位的方式(百度和系统自带的定位方式)

本文介绍了在iOS中获取用户定位的方法,包括设置info.plist文件,使用系统定位服务以及集成百度地图SDK获取经纬度。同时,文章涵盖了处理定位权限提示、更新位置信息以及如何获取城市名的细节。
摘要由CSDN通过智能技术生成

1. 首先在info.plist文件中设置

NSLocationAlwaysUsageDescription   string  我们需要您当前所在位置喔  //始终获取

NSLocationWhenInUseUsageDescription  string  我们需要您当前所在位置喔  //在使用应用期间

2. 设置系统和百度的定位方式

1) 系统定位

//头文件中加入delegate

@property (nonatomic,retain)CLLocationManager  *locationManager;

//.m文件中创建

//iOS自带定位,获取经纬度

- (void)createLocationManage

{

    self.locationManager = [[CLLocationManager alloc]init];

    self.locationManager.delegate = self;

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    self.locationManager.distanceFilter = 10;

    [CLLocationManager locationServicesEnabled];

    

    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {

        [self.locationManager requestAlwaysAuthorization];//如果想让ios8获取当前地址,需要添加此代码

    }

    [self.locationManager startUpdatingLocation];

    

    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {

        NSLog(@"定位不可用");

        [self shopsList:@" " setLat:@" "];

    }

}


#pragma mark CLLocationManager

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

    CLLocation *currLocation = [locations lastObject];

    

    localLongitude = currLocation.coordinate.longitude;

    localLatitude  = currLocation.coordinate.latitude;

    

    

    NSString *localLongitudeStr = [NSString stringWithFormat:@"%f",localLongitude]; //经度

    NSString *localLatitudeStr  = [NSString stringWithFormat:@"%f",localLatitude];  //维度

    

    //将经纬度保存到SharedInfo

    SharedInfo *sharedInfo = [SharedInfo sharedDataInfo];

    sharedInfo.localLongitude = localLongitudeStr;

    sharedInfo.localLatitude  = localLatitudeStr;

    

    

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值