{
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(@"定位服务当前可能尚未打开,请设置打开!");
return;
}
//如果没有授权则请求用户授权
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
[_locationManager requestWhenInUseAuthorization];
}else{
//设置代理
_locationManager.delegate = self;
//设置定位精度
_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(@"经度:%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(@"详细信息:%@",placemark.addressDictionary);
/*
SubLocality = 东城区,
CountryCode = CN,
Street = 鼓楼外大街,
State = 北京市,
Name = 中国北京市东城区和平里街道鼓楼外大街,
Thoroughfare = 鼓楼外大街,
FormattedAddressLines = [
中国北京市东城区和平里街道鼓楼外大街
],
Country = 中国,
City = 北京市
*/
_positioningV.place = [placemark.addressDictionary objectForKey:@"State"];
}];
}