iOS CLLocationManager定位
在iOS8以前的版本中,我们使用CLLocationManager定位是没有问题的,最近在iOS8系统中却无法定位了。。。。这是一大问题啊!
1、首先定义一个全局的变量用来记录CLLocationManager对象,引入CoreLocation.framework使用#import <CoreLocation/CoreLocation.h>
1 | @property (nonatomic, strong) CLLocationManager *locationManager; |
2、初始化CLLocationManager并开始定位
1 2 3 4 5 | locationManager=[[CLLocationManager alloc] init]; locationManager. delegate =self; locationManager.desiredAccuracy=kCLLocationAccuracyBest; locationManager.distanceFilter=10; [locationManager startUpdatingLocation]; //开启定位 |
3、实现CLLocationManagerDelegate的代理方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #pragma mark CLLocationManagerDelegate<br>/**<br>* 获取经纬度<br>*/ -( void )locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation *currLocation=[locations lastObject]; location.strLatitude=[NSString stringWithFormat: @"%f" ,currLocation.coordinate.latitude]; location.strLongitude=[NSString stringWithFormat: @"%f" ,currLocation.coordinate.longitude]; NSLog( @"la---%f, lo---%f" ,currLocation.coordinate.latitude,currLocation.coordinate.longitude);} /** *定位失败,回调此方法 */ -( void )locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ if ([error code]==kCLErrorDenied) { NSLog( @"访问被拒绝" ); } if ([error code]==kCLErrorLocationUnknown) { NSLog( @"无法获取位置信息" ); } } |
CLLocation *location=[[CLLocation alloc] initWithLatitude:currLocation.coordinate.latitude longitude:currLocation.coordinate.longitude];
CLGeocoder *getCoder=[[CLGeocoder alloc]init];
static NSString *strCity=@"";
[getCoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (error) {
NSLog(@"error: %@",error.description);
}
else{
// NSLog(@"placemarks count %lu",placemarks.count);
_lalAddress=[[UILabel alloc]initWithFrame:CGRectMake(100, HEIGHT-100, WIDTH-200, 30)];
[_lalAddress setTextAlignment:NSTextAlignmentCenter];
[_lalAddress setTextColor:[UIColor whiteColor]];
[_lalAddress setBackgroundColor:[UIColor blackColor]];
[_lalAddress setAlpha:0.8f];
// [self.window.rootViewController.view addSubview:_lalAddress];
for (CLPlacemark *placeMark in placemarks)
{
// NSLog(@"地址name:%@ ",placeMark.name);
// NSLog(@"地址thoroughfare:%@",placeMark.thoroughfare);
// NSLog(@"地址subThoroughfare:%@",placeMark.subThoroughfare);
// NSLog(@"地址locality:%@",placeMark.locality);
// NSLog(@"地址subLocality:%@",placeMark.subLocality);
// NSLog(@"地址administrativeArea:%@",placeMark.administrativeArea);
// NSLog(@"地址subAdministrativeArea:%@",placeMark.subAdministrativeArea);
// NSLog(@"地址postalCode:%@",placeMark.postalCode);
// NSLog(@"地址ISOcountryCode:%@",placeMark.ISOcountryCode);
// NSLog(@"地址country:%@",placeMark.country);
// NSLog(@"地址inlandWater:%@",placeMark.inlandWater);
// NSLog(@"地址ocean:%@",placeMark.ocean);
// NSLog(@"地址areasOfInterest:%@",placeMark.areasOfInterest);
strCity=placeMark.administrativeArea;
}
}
}];
iOS8中使用CoreLocation定位
1、在使用CoreLocation前需要调用如下函数【iOS8专用】:
iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法:
(1)始终允许访问位置信息
1 | - ( void )requestAlwaysAuthorization; |
(2)使用应用程序期间允许访问位置数据
1 | - ( void )requestWhenInUseAuthorization; |
示例如下:
1 2 3 4 5 6 7 8 | locationManager=[[CLLocationManager alloc] init]; locationManager. delegate =self; locationManager.desiredAccuracy=kCLLocationAccuracyBest; locationManager.distanceFilter=10; if (iOSVersion>=8) { [locationManager requestWhenInUseAuthorization]; //使用程序其间允许访问位置数据(iOS8定位需要) } [locationManager startUpdatingLocation]; //开启定位 |
2、在Info.plist文件中添加如下配置:
(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription

这样添加后,定位功能就能正常使用了!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)