LBS地圖定位

定位使用 " CoreLocation 框架
想要定位,需要使用5個步驟

1.首先創建一個"強引用"的位置管理器CLLocationManager

@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end

創建位置管理者對象及其賦值

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    self.locationManager = locationManager;

iOS8之后新增用戶授權 注意點:必須要配置plist文件
配置plist文件新增
NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysUsageDescription

始終使用用戶的位置 -->無論前臺還是后臺運行情況都能夠使用
當使用期間定位 -->只有在前臺運行情況才可以定位

位置過濾 單位:米 100.1表示當用戶位置更新了100.1米后調用對應的代理方法
期望精度 單位:米 100 :表示系統默認將100米看做同一個范圍

if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        [locationManager requestAlwaysAuthorization];
        [locationManager requestWhenInUseAuthorization];
    }
    locationManager.distanceFilter = 100.1;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

iOS9新特性-->后臺定位-->allowsBackgroundLocationUpdates
>當用戶授權為使用期間時,可以設置這個屬性為YES,在plist中添加"Required background modes" 在字典中添加值"App registers for location updates".

設置代理,開啟定位

    locationManager.delegate = self;
    [locationManager startUpdatingLocation];

實現代理方法
當定位到用戶位置時更新
一直定位 耗電,所以有時候需要停止定位
Locations:裝著CLLocation對象的數組
一個CLLocation代表一個位置

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    [manager stopUpdatingLocation];
    NSLog(@"%@",locations);
}

根據維度獲取位置下面分別是上海的位置和北京的位置 // 單位:米
根據比較可以得出倆地之間的距離

    CLLocation *location = [[CLLocation alloc] initWithLatitude:30 longitude:120];
    CLLocation *location1 = [[CLLocation alloc] initWithLatitude:39 longitude:115];
    double distance = [location distanceFromLocation:location1];

地理編碼和飯地理編碼

創建幾個控件

@property (weak, nonatomic) IBOutlet UITextField *longitudeTF;
@property (weak, nonatomic) IBOutlet UITextField *latitudeTF;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;

創建對象

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

地理編碼

    [geocoder geocodeAddressString:self.addressTF.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        // 防錯處理
        if (error) {
            NSLog(@"%@",error);
            return ;
        }
        // 提供一個列表供用戶選擇
        for (CLPlacemark *placemark in placemarks) {
            NSLog(@"name:%@   city:%@",placemark.name,placemark.locality);
            // 賦值
            // 緯度
            self.latitudeLabel.text = [NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude];
            // 經度
            self.longitudeLabel.text = [NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude];
            // 詳細地址
            self.detailLabel.text = placemark.name;
        }
        
    }];

反地理編碼

    // 2.1創建位置
    CLLocation *location = [[CLLocation alloc] initWithLatitude:[self.latitudeTF.text doubleValue] longitude:[self.longitudeTF.text doubleValue]];
    // 2.2調用方法  -->向蘋果請求數據
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        // 2.3防錯處理
        if (error) {
            NSLog(@"%@",error);
            return ;
        }
        // 2.4賦值 -->獲取地標
        CLPlacemark *placemark = placemarks[0];
        self.detailLabel.text = placemark.name;
    }];
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容