ios 苹果原生系统定位 CLLocationManager
首先要干这些事
下面的方法亲测可用
------------------------------------------------------------
DNLogFUNC
//初始化位置管理对象
_locationManager = [[CLLocationManager alloc] init];
//请求用户授权
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
[_locationManager requestWhenInUseAuthorization];
}
//指定定位代理类
_locationManager.delegate = self;
_locationManager.distanceFilter = 100;
//开启定位服务
[_locationManager startUpdatingLocation];
#pragma mark ---- CLLocationManagerDelegate
//定位成功 位置更新回调 的协议方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
CLLocation *currentLocation = [locations lastObject];
CLLocationCoordinate2D coor = currentLocation.coordinate;
//维度
DNLog(@"coor.latitude==%f",coor.latitude);
//经度
DNLog(@"coor.longitude==%f",coor.longitude);
// //地理编码类
// CLGeocoder *geocoder = [[CLGeocoder alloc] init];
// //反编码 经纬度点-->地理位置信息
// [geocoder reverseGeocodeLocation:[locations lastObject] completionHandler:^(NSArray *placemarks, NSError *error) {
// if (!error) {
// //取最后一个地址
// CLPlacemark *placemark = [placemarks lastObject];
// NSString *cityStr = [NSString stringWithFormat:@"%@",placemark.locality];
//// DNLog(@"cityStr==%@",cityStr);取出定位城市
// DNLog(@"placemark==%@",placemark.addressDictionary);
// DNLog(@"placemarks==%@",placemarks);
//
// }
// }];
}