sdk8.0以后,GPS定位的问题

1. 在 info.plist里加入对应的缺省字段 ,值设置为YES(前台定位写上边字段,前后台定位写下边字段)
NSLocationWhenInUseUsageDescription   //允许在前台获取GPS的描述

NSLocationAlwaysUsageDescription   //允许在前、后台获取GPS的描述

2. 调用请求:

//设置定位权限 仅ios8有意义
[locationManager requestWhenInUseAuthorization];// 前台定位

[locationManager requestAlwaysAuthorization];// 前后台同时定位

3. 添加坐标点:Product->Scheme->EditScheme

Options->Default Location->(选择一个地点)

4.添加Framework

MapKit.framework

CoreLocation.framework

代码片段

-(IBAction)StartGPS:(id)sender
{
    if (self.locationManager == nil) {
        self.locationManager = [[CLLocationManager alloc] init];
//        [self.locationManager requestAlwaysAuthorization];//前台和后台都要进行定位
        [self.locationManager requestWhenInUseAuthorization];//只有在前台的时候才进行定位
    }
    self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation];
}
- (IBAction)StopGPS:(id)sender
{
    [self.locationManager stopUpdatingLocation];
}
#pragma mark - CLLocationDelegate
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations objectAtIndex:0];
    [self.mapView setCenterCoordinate:location.coordinate animated:YES];
    //定义显示的范围
    MKCoordinateSpan theSpan;
    
    theSpan.latitudeDelta=0.1;
    
    theSpan.longitudeDelta=0.1;
    
    //定义一个区域(用定义的经纬度和范围来定义)
    
    MKCoordinateRegion theRegion;
    
    theRegion.center=location.coordinate;
    
    theRegion.span=theSpan;
    [self.mapView setRegion:theRegion animated:YES];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值