CoreLocation CLGeocoder 定位 反向地理編碼

{
    CLLocationManager       *_locationManager;
    CLGeocoder              *_geocoder;
}

@protocol

CLLocationManagerDelegate

PList

<key>NSLocationAlwaysUsageDescription</key>
    <string>定位請求</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>定位請求</string>
  _locationManager = [[CLLocationManager alloc]init];
    _geocoder = [[CLGeocoder alloc]init];
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"定位服務(wù)當(dāng)前可能尚未打開,請?jiān)O(shè)置打開!");
        return;
    }
    //如果沒有授權(quán)則請求用戶授權(quán)
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
        
        [_locationManager requestWhenInUseAuthorization];
        
    }else{
        //設(shè)置代理
        _locationManager.delegate = self;
        //設(shè)置定位精度
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        //定位頻率,每隔多少米定位一次
        CLLocationDistance distance=10.0;//十米定位一次
        _locationManager.distanceFilter=distance;
        [_locationManager startUpdatingLocation];
    }


#pragma mark - CoreLocation
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location=[locations firstObject];
    CLLocationCoordinate2D coordinate=location.coordinate;
    NSLog(@"經(jīng)度:%f,緯度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);

    [_locationManager stopUpdatingLocation];
    //[self getAddressByLatitude:coordinate.latitude longitude:coordinate.longitude];
    // 反向地理編碼
    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        CLPlacemark *placemark=[placemarks firstObject];
        NSLog(@"詳細(xì)信息:%@",placemark.addressDictionary);
        /*
        SubLocality = 東城區(qū),
        CountryCode = CN,
        Street = 鼓樓外大街,
        State = 北京市,
        Name = 中國北京市東城區(qū)和平里街道鼓樓外大街,
        Thoroughfare = 鼓樓外大街,
        FormattedAddressLines = [
        中國北京市東城區(qū)和平里街道鼓樓外大街
        ],
        Country = 中國,
        City = 北京市
         */
        _positioningV.place = [placemark.addressDictionary objectForKey:@"State"];
    }];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容