通過(guò)懶加載創(chuàng)建LocationManager:
// 懶加載
-
(CLLocationManager *)locationManager
{
if (_locationManager == nil) {
// 創(chuàng)建
_locationManager = [[CLLocationManager alloc] init];
// 設(shè)置代理
_locationManager.delegate = self;
// 每隔多少M(fèi) 定位一次
// _locationManager.distanceFilter = 100;/** kCLLocationAccuracyBestForNavigation // 最適合導(dǎo)航 kCLLocationAccuracyBest; // 最好的 kCLLocationAccuracyNearestTenMeters; // 10m kCLLocationAccuracyHundredMeters; // 100m kCLLocationAccuracyKilometer; // 1000m kCLLocationAccuracyThreeKilometers; // 3000m */ // 精度越高越耗電 _locationManager.desiredAccuracy = kCLLocationAccuracyBest; // 當(dāng)使用的時(shí)候 [_locationManager requestWhenInUseAuthorization]; // 一直使用
// [_locationManager requestAlwaysAuthorization];
}
return _locationManager;
}
locationManager的代理方法
// 獲取地理位置
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation > )locations
{
NSLog(@"定位到了");
NSLog(@"%@",locations.lastObject);
/- CLLocation 詳解
- coordinate : 經(jīng)緯度
- altitude : 海拔
- course : 航向
- speed ; 速度
*/
CLLocation *location = locations.lastObject;
// CLLocation *locationNew = [CLLocation alloc] initWithLatitude: longitude:<#(CLLocationDegrees)#>
[self.geocoder reverseGeocodeLocation:locations.lastObject completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placemark = placemarks.firstObject;
NSLog(@"%@",placemark.addressDictionary);
self.cityLabel.text = placemark.addressDictionary[@"City"];
self.streetLabel.text = placemark.addressDictionary[@"Street"];
self.guizhidaoLabel.text = placemark.addressDictionary[@"Name"];
}];
}
// 授權(quán)發(fā)生改變時(shí)會(huì)調(diào)用
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
// 用戶(hù)還未決定
case kCLAuthorizationStatusNotDetermined:
{
NSLog(@"用戶(hù)還未決定");
break;
}
// 訪問(wèn)受限
case kCLAuthorizationStatusRestricted:
{
NSLog(@"訪問(wèn)受限");
break;
}
// 定位關(guān)閉時(shí)和對(duì)此APP授權(quán)為never時(shí)調(diào)用
case kCLAuthorizationStatusDenied:
{
// 定位是否可用(是否支持定位或者定位是否開(kāi)啟)
if([CLLocationManager locationServicesEnabled])
{
NSLog(@"定位開(kāi)啟蘑斧,但被拒");
}else
{
NSLog(@"定位關(guān)閉竖瘾,不可用");
}
// NSLog(@"被拒");
break;
}
// 獲取前后臺(tái)定位授權(quán)
case kCLAuthorizationStatusAuthorizedAlways:
// case kCLAuthorizationStatusAuthorized: // 失效花颗,不建議使用
{
NSLog(@"獲取前后臺(tái)定位授權(quán)");
break;
}
// 獲得前臺(tái)定位授權(quán)
case kCLAuthorizationStatusAuthorizedWhenInUse:
{
NSLog(@"獲得前臺(tái)定位授權(quán)");
break;
}
default:
break;
}
}
pragma mark 根據(jù)地名確定地理坐標(biāo)
-(void)getCoordinateByAddress:(NSString *)address{
//地理編碼
[_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
//取得第一個(gè)地標(biāo)乐横,地標(biāo)中存儲(chǔ)了詳細(xì)的地址信息今野,注意:一個(gè)地名可能搜索出多個(gè)地址
CLPlacemark *placemark=[placemarks firstObject];
CLLocation *location=placemark.location;//位置
CLRegion *region=placemark.region;//區(qū)域
NSDictionary *addressDic= placemark.addressDictionary;//詳細(xì)地址信息字典,包含以下部分信息
// NSString *name=placemark.name;//地名
// NSString *thoroughfare=placemark.thoroughfare;//街道
// NSString *subThoroughfare=placemark.subThoroughfare; //街道相關(guān)信息条霜,例如門(mén)牌等
// NSString *locality=placemark.locality; // 城市
// NSString *subLocality=placemark.subLocality; // 城市相關(guān)信息宰睡,例如標(biāo)志性建筑
// NSString *administrativeArea=placemark.administrativeArea; // 州
// NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政區(qū)域信息
// NSString *postalCode=placemark.postalCode; //郵編
// NSString *ISOcountryCode=placemark.ISOcountryCode; //國(guó)家編碼
// NSString *country=placemark.country; //國(guó)家
// NSString *inlandWater=placemark.inlandWater; //水源拆内、湖泊
// NSString *ocean=placemark.ocean; // 海洋
// NSArray *areasOfInterest=placemark.areasOfInterest; //關(guān)聯(lián)的或利益相關(guān)的地標(biāo)
NSLog(@"位置:%@,區(qū)域:%@,詳細(xì)信息:%@",location,region,addressDic);
}];
}
pragma mark 根據(jù)坐標(biāo)取得地名
-(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{
//反地理編碼
CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
[_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark=[placemarks firstObject];
NSLog(@"詳細(xì)信息:%@",placemark.addressDictionary);
}];
}