美文网首页
苹果自带定位获取位置

苹果自带定位获取位置

作者: 张_何 | 来源:发表于2019-08-15 14:09 被阅读0次

    前言

    • 之前项目一直用苹果定位获取经纬度后把经纬度信息传给腾讯地图接口来获取用户位置信息(NSString * location_qq_api_key = @"腾讯给的key字符串";
      NSURL * url =[NSURL URLWithString:[NSString stringWithFormat:@"https://apis.map.qq.com/ws/geocoder/v1?coord_type=1&key=%@&location=%f,%f",location_qq_api_key,_bestEffortAtLocation.coordinate.latitude,_bestEffortAtLocation.coordinate.longitude]];)
      导致我一直以为,苹果不能获取街道信息,其实想想苹果既然有自带地图肯定也可以获取详细的位置信息。

    代码示例

    /*
     准备工作:
     先在info.plist文件中设置
     Privacy - Location When In Use Usage Description
     Privacy - Location Always and When In Use Usage Description
     Privacy - Location Usage Description
     */
    #import "LocationVC.h"
    #import <CoreLocation/CoreLocation.h>
    
    @interface LocationVC()<CLLocationManagerDelegate>
    
    @property(nonatomic,strong) CLLocationManager *locationManager;
    
    @end
    
    @implementation LocationVC
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        //设置精度
        [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        // 请求定位权限
        [self.locationManager requestAlwaysAuthorization];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
        if (status == kCLAuthorizationStatusAuthorized || status == kCLAuthorizationStatusAuthorizedWhenInUse || status ==  kCLAuthorizationStatusAuthorizedAlways ) {
            [self.locationManager startUpdatingLocation];
        } else if (status == kCLAuthorizationStatusDenied) {
           
        }
    }
    
    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    //    获取最新的定位信息
        CLLocation *currentLocation = locations.lastObject;
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            if (placemarks.count > 0) {
                CLPlacemark *placemark = placemarks.firstObject;
                NSString *city = placemark.locality; // 市
            //有些人说如果是直辖市的h话通过placemark.locality获取到的是空,要通过placemark.administrativeArea来获取,但我试验的结果并非如此
                if (!city) {
                    city = placemark.administrativeArea;
                }
                NSString *name = placemark.name;  // 具体位置
                NSString *thoroughfare = placemark.thoroughfare;  //街道
                NSString *subThoroughfare = placemark.subThoroughfare;//子街道(门牌号)
                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<NSString *> *areasOfInterest = placemark.areasOfInterest;// 关联的或利益相关的地标
                NSDictionary *addressDictionary = placemark.addressDictionary;// 位置信息的字典
                NSLog(@"-------\ncity = %@\nname = %@\nthoroughfare = %@\nsubThoroughfare = %@\nsubLocality = %@\nadministrativeArea = %@\nsubAdministrativeArea = %@\npostalCode = %@\nISOcountryCode = %@\ncountry = %@\ninlandWater = %@\nocean = %@\nareasOfInterest = %@\naddressDictionary = %@", city, name, thoroughfare, subThoroughfare, subLocality, administrativeArea, subAdministrativeArea, postalCode, ISOcountryCode, country, inlandWater, ocean, areasOfInterest, addressDictionary);
            }
        }];
        // 不停止的话会一直调用
        [self.locationManager stopUpdatingHeading];
    }
    

    输出信息

     city = 上海市 
     name = 中国工商银行(陆家嘴软件园支行)   
     thoroughfare = 峨山路91弄98号近东方路
     subThoroughfare = (null)
     subLocality = 浦东新区
     administrativeArea = (null)
     subAdministrativeArea = (null)
     postalCode = (null)
     ISOcountryCode = CN
     country = 中国
     inlandWater = (null)
     ocean = (null)
     areasOfInterest = (null)
     addressDictionary = {
        City = "上海市";
         Country = "中国";
         CountryCode = CN;
         FormattedAddressLines =     (
         "中国上海市浦东新区峨山路91弄98号近东方路"
         );
         Name = "中国工商银行(陆家嘴软件园支行)";
         Street = "峨山路91弄98号近东方路";
         SubLocality = "浦东新区";
         Thoroughfare = "峨山路91弄98号近东方路";
     }
    

    相关文章

      网友评论

          本文标题:苹果自带定位获取位置

          本文链接:https://www.haomeiwen.com/subject/uoaajctx.html