CLLocationManager定位当前位置

1、导入CoreLocation.h 头文件,然后声明专属的管理者对象,遵守代理CLLocationManagerDelegate

@property(nonatomic,strong)CLLocationManager *locationManager;

2、在viewdidload里面判断当前设备定位服务是否打开了,然后设置代理和精度

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//初始化管理对象

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

self.locationManager = locationManager;

//判断当前设备定位服务是否打开

if (![CLLocationManager locationServicesEnabled]) {

NSLog(@"设备尚未打开定位服务");

}

//判断当前设备版本大于iOS8以后的话执行里面的方法

if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {

[locationManager requestAlwaysAuthorization];

[locationManager  requestWhenInUseAuthorization];

}

//设置代理

locationManager.delegate = self;

//设置定位精度

locationManager.desiredAccuracy = kCLLocationAccuracyBest;

////设置定位的频率,这里我们设置精度为10,也就是10米定位一次

CLLocationDistance distance = 10;

//给精度赋值

locationManager.distanceFilter = distance;

//开始启动定位

[locationManager startUpdatingLocation];

}


3、配置info.plist文件,把下面两个属性加到文件中去,并且确定xcode是否联网

NSLocationAlwaysUsageDescription

NSLocationWhenInUseUsageDescription



4、实现代理方法进行定位当前的位置,获取到经纬度

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)locations

5、通过反地理编码进行中文的输出即可

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {


6.不定位的时候应该停止定位

[self.locationManager stopUpdatingLocation];

//定位获取当前的经纬度

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)locations {    CLLocation *location = [locations lastObject];    NSLog(@"%@",location.timestamp);    CLLocationCoordinate2D coordinate = location.coordinate;    NSLog(@"%f    ,%f", coordinate.latitude,coordinate.longitude );        CLGeocoder *geocoder = [[CLGeocoder alloc]init];        [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {

if (error != nil || placemarks.count == 0) {

NSLog(@"%@",error);

return ;

}

for (CLPlacemark *placemark in placemarks) {

self.addressTextLabel.text = placemark.locality;

NSLog(@"%@",placemark.locality);

}

}];

[self.locationManager stopUpdatingLocation];

}



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • http://www.cnblogs.com/kenshincui/p/4125570.html 摘要: 现在很多...
    大崔老师阅读 8,598评论 1 2
  • 出自http://my.oschina.net/are1OfBlog/blog/420034 摘要 现在很多社交、...
    JJO阅读 9,641评论 4 19
  • 一、定位介绍 现在很多社交、电商、团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的。的...
    LHsunshine阅读 2,778评论 0 0
  • 看到那句话颇有感触,你有多久没有好好写一篇文章了,回头想来真正用心写过的只有哪些年的情书和近年来写给老婆的情诗了,...
    南村一庙阅读 1,328评论 0 0
  • 01 很多需要在朋友圈发广告做生意的人, 会不断地通过一些途径去添加好友; 我的朋友圈里等你接受通过的信息就犹如一...
    遇见阿锋阅读 6,514评论 0 0

友情链接更多精彩内容