定位城市+iOS8 定位问题

在IOS8中定位功能新增了两个方法:


- (void)requestWhenInUseAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);- (void)requestAlwaysAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);



这两个新增的方法导致,之前写的程序在iOS8运行会出现,定位功能无法正常使用

这样让iOS8正常使用定位功能呢?

<1>你需要在info.plist表里面添加两条变量

在Info.plist中加入两个缺省没有的字段

  • NSLocationAlwaysUsageDescription

  • NSLocationWhenInUseUsageDescription

这两个字段没什么特别的意思,就是自定义提示用户授权使用地理定位功能时的提示语。



这样在写代码:

    CLLocationManager  *locationManager = [[CLLocationManager alloc]init];    locationManager.delegate = self;    [locationManager requestAlwaysAuthorization];    locationManager.desiredAccuracy = kCLLocationAccuracyBest;    locationManager.distanceFilter = kCLDistanceFilterNone;    [locationManager startUpdatingLocation];


这是在调用代理


- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {    switch (status) {        case kCLAuthorizationStatusNotDetermined:            if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {                [locationManager requestWhenInUseAuthorization];            }            break;        default:            break;    }}


这样就Ok了,就会弹出原来的提示框





以下是全部的实现代码:


导入头文件   #import <CoreLocation/CoreLocation.h>

设置代理方法:

@interface PositioningController ()<CLLocationManagerDelegate>{

    UILabel * addressLabel;

}

//定位

@property(nonatomic,strong)CLLocationManager * locationManager;

//地址解析器

@property(nonatomic,strong)CLGeocoder * geocoder;

@end


@implementation PositioningController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    self.locationManager.delegate =self;

    [self.locationManagerrequestAlwaysAuthorization];

    self.locationManager.desiredAccuracy =kCLLocationAccuracyBest;

    self.locationManager.distanceFilter =kCLDistanceFilterNone;

    [self.locationManagerstartUpdatingLocation];

    //开始定位

    [self.locationManagerstartUpdatingLocation];

    [selfcreateUI];

}

#pragma mark -懒加载

-(CLGeocoder *)geocoder{

    

    if (_geocoder ==nil) {

        

        _geocoder =[[CLGeocoderalloc]init];

    }

    return_geocoder;

}

-(void)createUI{


    addressLabel =[[UILabelalloc]initWithFrame:CGRectMake(20,50, 100,50)];

    addressLabel.backgroundColor =[UIColorlightGrayColor];

    [self.viewaddSubview:addressLabel];


}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

    switch (status) {

        casekCLAuthorizationStatusNotDetermined:

        if ([self.locationManagerrespondsToSelector:@selector(requestAlwaysAuthorization)]) {

            [self.locationManagerrequestWhenInUseAuthorization];

        }

        break;

        default:

        break;

    }

}

-(CLLocationManager *)locationManager{

    

    if (_locationManager ==nil) {

        

        //实例化定位对象

        _locationManager = [[CLLocationManageralloc]init];

        

        //2.判断定位服务是否可用

        if ([CLLocationManagerlocationServicesEnabled]) {

            

            //3.设置总是打开定位服务

            //代码打开,设置plist文件

            

            if ([UIDevicecurrentDevice].systemVersion.floatValue >=8.0) {

                

                [_locationManagerrequestAlwaysAuthorization];

            }

            //4.设置距离(多大距离刷新一次定位信息)单位:米

            _locationManager.distanceFilter  =10000;

            //5.设置代理

            _locationManager.delegate =self;

        }else{

            

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

        }

    }

    return_locationManager;

}

-(void)parsingAddress{


    

}

#pragma mark -locationManager Delegate

//当位置刷新的时候会多次调用这个方法

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

    

    CLLocation  *location =locations[0];

    //拿到经纬度

    CLLocationCoordinate2D coord = location.coordinate;

    NSLog(@"%f",coord.latitude);

    NSLog(@"%f",coord.longitude);


    //参数:1.需要反编码的位置 2.编码结束后调用的block

    [self.geocoderreverseGeocodeLocation:location completionHandler:^(NSArray *placemarks,NSError *error) {

        

        if (placemarks.count >0) {

            

            CLPlacemark * place =placemarks[0];

            

            //存储地址信息的字典

            //place.addressDictionary =

            NSLog(@"%@",place.addressDictionary[@"FormattedAddressLines"][0]);

            //1.显示反解析得到的地址

            addressLabel.text =place.addressDictionary[@"FormattedAddressLines"][0];

    

        }else{

            

            NSLog(@"没有取到地址");

        }

    }];

   [self.locationManagerstopUpdatingLocation];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值