iOS CLLocationManager定位
来源:互联网 发布:初级会计怎么备考 知乎 编辑:程序博客网 时间:2024/06/09 15:43
在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
18
#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(
@"无法获取位置信息"
);
}
}
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
这样添加后,定位功能就能正常使用了!
0 0
- ios定位CLLocationManager
- ios CLLocationManager定位
- iOS CLLocationManager定位
- iOS CLLocationManager定位
- iOS CLLocationManager定位
- iOS--CLLocationManager 定位
- iOS CLLocationManager定位,IOS8注意
- Ios开发之定位CLLocationManager
- iOS开发之定位CLLocationManager
- IOS Swift3 使用 CLLocationManager 定位
- IOS:ios8.0下CLLocationManager定位服务
- IOS CLLocationManager 设置定位请求的方式
- 定位 (CLLocationManager )
- CLLocationManager 定位
- 定位CLLocationManager
- 定位 CLLocationManager
- IOS开发教程--Xcode6,CLLocationManager无法定位解决方案
- 关于IOS定位和CLLocationManager 小知识点总结
- eclipse -- debug增加条件的用法
- sqlserver查询分析器执行sql语句顺序
- ios8.0下CLLocationManager定位服务需要授权了
- spring2.5 xml以及注解的简单入门示例(ioc)
- Struts 2 Hello World Example
- iOS CLLocationManager定位
- 百度2015 在线笔试题(2)
- Android Studio删除module
- After 500: 写了第500篇博客,然后呢?
- 关于Spring中的<idref/>
- UIPickerView 学习
- tcp/ip 连接三次握手,断开四次握手
- HDU 5430:Reflect 欧拉函数
- 电脑上使环境变量立即生效的方法