快好知 kuaihz订阅观点

 

IOS开发笔记整理

IOS开发笔记整理

在项目功能中有一个定位CLLocation的需求,遇到了一些知识难点,经过各位大侠的帮助,问题解决,特此分享供大家学习,希望大家共同学习进步。

一、简单说明

1.CLLocationManager

CLLocationManager的常用操作和属性

开始用户定位- (void)startUpdatingLocation;

停止用户定位- (void) stopUpdatingLocation;

说明:当调用了startUpdatingLocation方法后,就开始不断地定位用户的`位置,中途会频繁地调用代理的下面方法

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

每隔多少米定位一次

@property(assign, nonatomic) CLLocationDistance distanceFilter;

定位精确度(越精确就越耗电)

@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;

使用定位功能,首先要导入框架,遵守CLLocationManagerDelegate协议,再创建位置管理器CLLocationManager

在iOS8.0后,定位功能需要在info.plist中加入NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription这两个NSString类型字段,才能够使用定位功能

代码贴出来与大家共勉,各位看官自行研究

{ self.locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; if([CLLocationManager locationServicesEnabled] == NO) { // NSLog(@"没有GPS服务"); } //地理位置精确度 _locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters; //设置距离筛选器,double类型,只要距离变化多少,就调用委托代理 self.locationManager.distanceFilter = kCLDistanceFilterNone; // meters [_locationManager requestWhenInUseAuthorization];// 前台定位 [_locationManager startUpdatingLocation];}- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ NSLog(@"longitude = %f", ((CLLocation *)[locations lastObject]).coordinate.longitude); NSLog(@"latitude = %f", ((CLLocation *)[locations lastObject]).coordinate.latitude); CGFloat longTI=((CLLocation *)[locations lastObject]).coordinate.longitude; CGFloat latTI=((CLLocation *)[locations lastObject]).coordinate.latitude; //将经度显示到label上 _longitudeLabel.text = [NSString stringWithFormat:@"%f",longTI]; //将纬度现实到label上 _latitudeLabel.text = [NSString stringWithFormat:@"%f",latTI]; // 获取当前所在的城市名 CLGeocoder *geocoder = [[CLGeocoder alloc] init]; //根据经纬度反向地理编译出地址信息 [geocoder reverseGeocodeLocation:locations.lastObject completionHandler:^(NSArray *array, NSError *error) { if (array.count > 0) { CLPlacemark *placemark = [array objectAtIndex:0];// //将获得的所有信息显示到label上// self.location.text = placemark.name; //获取城市 NSString *city = placemark.locality; if (!city) { //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市) city = placemark.administrativeArea; } // NSLog(@"city = %@", city); _cityName=city; } else if (error == nil && [array count] == 0) { // NSLog(@"No results were returned."); } else if (error != nil) { // NSLog(@"An error occurred = %@", error); } }]; //系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新 [manager stopUpdatingLocation];}

以上是关于小编给大家整理的IOS开发之详解定位CLLocation,后续还会持续更新,希望大家能够喜欢。

本站资源来自互联网,仅供学习,如有侵权,请通知删除,敬请谅解!
搜索建议:IOS开发笔记整理  整理  整理词条  笔记  笔记词条  开发  开发词条  IOS  IOS词条  
综合

 移动手机最新刷钻方法

移动手机最新刷钻方法现在智能手机的功能是十分强大的,可以帮助我们实现很多功能。那么移动手机最新刷钻方法是什么呢?下面就让jy135小编来告诉大家吧,欢迎阅读。发...(展开)

综合

 iPhone6从icloud恢复...

iPhone6从icloud恢复备份的方法很多使用iPhone6的用户都不知道icloud如何恢复备份,小编下面就给大家带来iPhone6从icloud恢复备份...(展开)

综合

 小米恢复出厂设置

小米恢复出厂设置对于安卓设备用户来说,“恢复出厂设置”一词一定不会陌生。在安卓设备系统出现了严重故障或者无法正常使用的时候,很多用户都会选择进行这一操作,不过这...(展开)