IOS8 定位,地理编码,反地理编码



一、修改info

在iOS8以前,app第一次开始定位服务时,系统会弹出一个提示框来让用户选择是否容许使用定位信息。但iOS8后,app将不会出现这个弹窗。第一次运行以后,在设置->隐私->定位服务中,你的app没有任何设置,既不是“永不”,也不是“始终”。php


IOS8新增Key:NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription,这两个Key的值将分别用于描述应用程序始终使用和使用期间使用定位的说明,这些说明将显示在用户设置中。 info新增键值对以下: \
\html

NSLocationAlwaysUsageDescription/NSLocationWhenInUseUsageDescription的值,这个值会显示在系统提示框中。ios

两个KEY至少有一个,后面的描述能够不写或本身随意写。git

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


对应的方法:    在初使化_locationManager 时使用一个(iOS8CLLocationManager新增实例方法post

    [_locationManager requestWhenInUseAuthorization];//使用时打开ui

    [_locationManager requestAlwaysAuthorization];//始终打开编码

   [_locationManager startUpdatingLocation];
atom



二、CLLocationManager初始化

#import <CoreLocation/CoreLocation.h>url

@interface nextViewController : UIViewController<CLLocationManagerDelegate>

@property(nonatomic, strong) CLLocationManager *locationManager;

@property CLLocation *currLocation ;

@property CLGeocoder *geocoder;


- (void) location{

    _locationManager= [[CLLocationManager alloc]init];

    _locationManager.delegate = self;

    [_locationManager requestAlwaysAuthorization];

    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    _locationManager.distanceFilter = kCLDistanceFilterNone;

    [_locationManager requestWhenInUseAuthorization]; //ios8 新增方法

    [_locationManager startUpdatingLocation];

}


三、代理

#pragma mark - CoreLocation 代理

#pragma mark 跟踪定位代理方法,每次位置发生变化即会执行(只要定位到相应位置)

//能够经过模拟器设置一个虚拟位置,不然在模拟器中没法调用此方法

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

    CLLocation *location=[locations firstObject];//取出第一个位置

    CLLocationCoordinate2D coordinate=location.coordinate;//位置坐标

    NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);

    //若是不须要实时定位,使用完即便关闭定位服务

    [_locationManager stopUpdatingLocation];

    

    _currLocation = location; //全局location  用于反理理编码

    [self reverseGeocode1];

}

//失败 报错

-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{

    NSLog(@"error:%@",error);

}

//另外一个经常使用的代理

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

    switch (status) {

        case kCLAuthorizationStatusNotDetermined:

        if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {

            [_locationManager requestWhenInUseAuthorization];

        }

            break;

        default:

        break;

    }

}


四、反地理编码

- (void)reverseGeocode1{

   // 根据坐标取得地名

    NSString *longtitudeText = @"114.10";

    NSString *latitudeText = @"28.19";  

    CLLocationDegrees latitude = [latitudeText doubleValue];

    CLLocationDegrees longitude = [longtitudeText doubleValue];


    //注意初使化location参数 double转换,这里犯过错,    此外通常会用 全局_currLocation,在定位后获得

   CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];

    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

        CLPlacemark *placemark=[placemarks firstObject];

        NSLog(@"详细信息:%@",placemark.addressDictionary);

    }];

}

五、地理编码

#pragma mark 地理编码 根据地名肯定地理坐标

-(void)getCoordinateByAddress:(NSString *)address{

    //地理编码

    [_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {

        //取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址

        CLPlacemark *placemark=[placemarks firstObject];

        CLLocation *location=placemark.location;//位置

        CLRegion *region=placemark.region;//区域

        NSDictionary *addressDic= placemark.addressDictionary;//详细地址信息字典,包含如下部分信息

//        NSString *name=placemark.name;//地名

//        NSString *thoroughfare=placemark.thoroughfare;//街道

//        NSString *subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等

//        NSString *locality=placemark.locality; // 城市

//        NSString *subLocality=placemark.subLocality; // 城市相关信息,例如标志性建

//        NSString *administrativeArea=placemark.administrativeArea; //

//        NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其余行政区域信

//        NSString *postalCode=placemark.postalCode; //邮编

//        NSString *ISOcountryCode=placemark.ISOcountryCode; //国家编码

//        NSString *country=placemark.country; //国家

//        NSString *inlandWater=placemark.inlandWater; //水源、湖泊

//        NSString *ocean=placemark.ocean; // 海洋

//        NSArray *areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标

        NSLog(@"位置:%@,区域:%@,详细信息22222222:%@",location,region,addressDic);

    }];

}

详细的定位说时,能够看:http://www.cnblogs.com/kenshincui/p/4125570.html

相关文章
相关标签/搜索