定位CoreLocation

  • CoreLocation用于獲取設(shè)備的地理位置信息和方向

定位

//用于定位的框架
#import <CoreLocation/CoreLocation.h>
//CoreLocation框架中所有數(shù)據(jù)類型的前綴都是CL
//CoreLocation中使用CLLocationManager對象來做用戶定位
//位置管理者
@property (nonatomic,strong) CLLocationManager *locationManager;
//--------------------------------------------------------------------
//1.創(chuàng)建位置管理者
    CLLocationManager *manager = [[CLLocationManager alloc] init];
    //記錄成員變量
    self.locationManager = manager;
//在iOS8之前,只需要導(dǎo)入CoreLocation 會自動申請權(quán)限
    //在iOS8之后需要程序員手寫
    //2.請求用戶授權(quán)  必須要配置info.plist文件
    //請求app始終授權(quán) 無論程序在前臺還是在后臺運行 都可以定位
//    [manager requestAlwaysAuthorization];
    //請求app在使用期間授權(quán) 在前臺使用時才可以使用定位
    [manager requestWhenInUseAuthorization];
    
    if([UIDevice currentDevice].systemVersion.floatValue >= 9.0) {
        //臨時開啟后臺定位 配置info.plist文件 不配置直接崩潰
        manager.allowsBackgroundLocationUpdates = YES;
    }

<key>NSLocationWhenInUseUsageDescription</key>
    <string></string>
 //設(shè)置定位屬性
    //每隔多遠定位一次
    manager.distanceFilter = 100;
    /*
      kCLLocationAccuracyBestForNavigation //最適合導(dǎo)航
      kCLLocationAccuracyBest;//最好的
      kCLLocationAccuracyNearestTenMeters;//10米
      kCLLocationAccuracyHundredMeters;//100米
      kCLLocationAccuracyKilometer;//1000米
      kCLLocationAccuracyThreeKilometers;//3000米
     */
    //精確度越高,越耗電,定位時間越長
    manager.desiredAccuracy = kCLLocationAccuracyBest;
//設(shè)置代理
    manager.delegate = self;
//開啟定位
    [manager startUpdatingLocation];
#pragma mark - CLLocationManagerDelegate
/*
 更新位置之后調(diào)用
 參數(shù)1:位置管理者
 參數(shù)2:位置數(shù)組
 */
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    //獲取數(shù)據(jù)
    NSLog(@"%@",locations);
    //停止定位 省電
    [manager stopUpdatingLocation];
}

比較兩點之間的直線距離

//比較兩個位置之間的距離
-(void)compareDistance {
    //根據(jù)精度生成位置信息
    CLLocation *location1 = [[CLLocation alloc] initWithLatitude:39 longitude:115];
    
    CLLocation *location2 = [[CLLocation alloc] initWithLatitude:30 longitude:120];
    
    //計算距離
    CLLocationDistance distance = [location1 distanceFromLocation:location2];
    
    NSLog(@"%.2fKm",distance/1000);
}

地理編碼 反地理編碼

    name                    地名
    thoroughfare            街道
    subThoroughfare         街道相關(guān)信息,例如門牌等
    locality                城市
    subLocality             城市相關(guān)信息,例如標志性建筑
    administrativeArea      直轄市
    subAdministrativeArea   其他行政區(qū)域信息(自治區(qū)等)
    postalCode              郵編
    ISOcountryCode          國家編碼
    country                 國家
    inlandWater             水源,湖泊
    ocean                   海洋
//地理編碼
- (IBAction)geocoder:(id)sender {
    //地理編碼:將具體地址轉(zhuǎn)換成經(jīng)緯度
    
    //1.創(chuàng)建地理編碼對象
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //2.地理編碼
    if(self.addressTextField.text.length == 0) return;
    //異步 向蘋果請求數(shù)據(jù)
    [geocoder geocodeAddressString:self.addressTextField.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        //防錯處理
        if(error){
            NSLog(@"%@",error);
            return ;
        }
        //placemarks:所有的地標 一個CLPlacemark代表一個地標
        for (CLPlacemark *placemark in placemarks) {
            NSLog(@"%@",placemark);
            //精度
            self.longitudeLabel.text = [NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude];
            //緯度
            self.latitudeLabel.text = [NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude];
            
        }
    }];
}
//反地理編碼
- (IBAction)revGeocoder:(id)sender {
    //反地理編碼: 將經(jīng)緯度轉(zhuǎn)換成具體地址
    
    //1.創(chuàng)建地理編碼對象
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //2.反地理編碼
    //2.1創(chuàng)建位置信息
    CLLocation *location = [[CLLocation alloc] initWithLatitude:[self.longitudeTextField.text floatValue] longitude:[self.latitudeTextField.text floatValue]];
    //2.2向蘋果請求數(shù)據(jù)
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        //2.3防錯處理
        if(error){
            NSLog(@"%@",error);
            return ;
        }
        //2.4獲取地標 在公司開發(fā)中給一個列表供用戶選擇
        CLPlacemark *placemark = placemarks[0];
        //2.5賦值
        self.addressLabel.text = placemark.name;
        NSLog(@"%@",placemark.locality);
    }];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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