定位使用 " CoreLocation 框架
想要定位辽社,需要使用5個步驟
1.首先創(chuàng)建一個"強引用"的位置管理器CLLocationManager
@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
創(chuàng)建位置管理者對象及其賦值
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
self.locationManager = locationManager;
iOS8之后新增用戶授權(quán) 注意點:必須要配置plist文件
配置plist文件新增
NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysUsageDescription
始終使用用戶的位置 -->無論前臺還是后臺運行情況都能夠使用
當(dāng)使用期間定位 -->只有在前臺運行情況才可以定位
位置過濾 單位:米 100.1表示當(dāng)用戶位置更新了100.1米后調(diào)用對應(yīng)的代理方法
期望精度 單位:米 100 :表示系統(tǒng)默認將100米看做同一個范圍
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
[locationManager requestAlwaysAuthorization];
[locationManager requestWhenInUseAuthorization];
}
locationManager.distanceFilter = 100.1;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
iOS9新特性-->后臺定位-->allowsBackgroundLocationUpdates
>當(dāng)用戶授權(quán)為使用期間時亏娜,可以設(shè)置這個屬性為YES需了,在plist中添加"Required background modes" 在字典中添加值"App registers for location updates".
設(shè)置代理,開啟定位
locationManager.delegate = self;
[locationManager startUpdatingLocation];
實現(xiàn)代理方法
當(dāng)定位到用戶位置時更新
一直定位 耗電,所以有時候需要停止定位
Locations:裝著CLLocation對象的數(shù)組
一個CLLocation代表一個位置
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
[manager stopUpdatingLocation];
NSLog(@"%@",locations);
}
根據(jù)維度獲取位置下面分別是上海的位置和北京的位置 // 單位:米
根據(jù)比較可以得出倆地之間的距離
CLLocation *location = [[CLLocation alloc] initWithLatitude:30 longitude:120];
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:39 longitude:115];
double distance = [location distanceFromLocation:location1];
地理編碼和飯地理編碼
創(chuàng)建幾個控件
@property (weak, nonatomic) IBOutlet UITextField *longitudeTF;
@property (weak, nonatomic) IBOutlet UITextField *latitudeTF;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
創(chuàng)建對象
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];
// 經(jīng)度
self.longitudeLabel.text = [NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude];
// 詳細地址
self.detailLabel.text = placemark.name;
}
}];
反地理編碼
// 2.1創(chuàng)建位置
CLLocation *location = [[CLLocation alloc] initWithLatitude:[self.latitudeTF.text doubleValue] longitude:[self.longitudeTF.text doubleValue]];
// 2.2調(diào)用方法 -->向蘋果請求數(shù)據(jù)
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// 2.3防錯處理
if (error) {
NSLog(@"%@",error);
return ;
}
// 2.4賦值 -->獲取地標(biāo)
CLPlacemark *placemark = placemarks[0];
self.detailLabel.text = placemark.name;
}];