苹果使用的cllocationmanager是谷歌tag manager地图么

地图开发(2)
#import &ViewController.h&
#import &CoreLocation/CoreLocation.h&
@interface ViewController () &CLLocationManagerDelegate&
@property (nonatomic, strong) CLLocationManager *locationM
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.第一步
// 在Info.plist文件中添加如下两个字段中的至少一个
// NSLocationWhenInUseUsageDescription
// NSLocationAlwaysUsageDescription
// 如果两个都设置,取NSLocationAlwaysUsageDescription的描述(描述可空)
// 开始定位
[self.locationManager startUpdatingLocation];
- (CLLocationManager *)locationManager {
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc] init];
[_locationManager requestAlwaysAuthorization]; // 2.必须设置
_locationManager.delegate =
return _locationM
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray&CLLocation *& *)locations {
CLLocation *location = [locations firstObject];
NSLog(@&latitude = %f, longitude = %f&, location.coordinate.latitude, location.coordinate.longitude);
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:144602次
积分:2803
积分:2803
排名:第12652名
原创:131篇
转载:88篇
评论:15条
(2)(7)(8)(1)(1)(5)(8)(15)(22)(17)(3)(2)(1)(10)(11)(2)(12)(8)(2)(2)(2)(1)(2)(5)(6)(2)(5)(3)(13)(7)(2)(4)(3)(5)(13)(2)(4)iOS CLLocationManager定位,IOS8注意
今天下午动手用了自带的定位,结果在网上看了很多教程,也将示例代码直接运行,但就是一直无法获取位置,代码如下:
首先导入CoreLocation.framework,然后再引入头文件#import
@property (nonatomic , strong)CLLocationManager *locationM
然后使用代理 CLLocationManagerDelegate
- (void)locate{
// 判断定位操作是否被允许
if([CLLocationManager locationServicesEnabled]) {
//定位初始化
_locationManager=[[CLLocationManager alloc] init];
_locationManager.delegate=
_locationManager.desiredAccuracy=kCLLocationAccuracyB
_locationManager.distanceFilter=10;
[_locationManager startUpdatingLocation];//开启定位
//提示用户无法进行定位操作
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@提示 message:@定位不成功 ,请确认开启定位 delegate:nil cancelButtonTitle:@取消 otherButtonTitles:@确定, nil];
[alertView show];
// 开始定位
[_locationManager startUpdatingLocation];
#pragma mark - CoreLocation Delegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
//此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
CLLocation *currentLocation = [locations lastObject];
// 获取当前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//根据经纬度反向地理编译出地址信息
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error)
if (array.count & 0)
CLPlacemark *placemark = [array objectAtIndex:0];
//NSLog(@%@,placemark.name);//具体位置
//获取城市
NSString *city = placemark.
if (!city) {
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeA
cityName =
NSLog(@定位完成:%@,cityName);
//会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
[manager stopUpdatingLocation];
}else if (error == nil && [array count] == 0)
NSLog(@No results were returned.);
}else if (error != nil)
NSLog(@An error occurred = %@, error);
在viewDidLoad中调用了locate之后一直没有数据返回,代理也不执行,在网上找了很多教程,代码大多大同小异,但是结果都是不理想,偶然之间看到ios8的定位和低版本的不同,我的手机刚好是ios8,便点开那个博客去看看,博客请点此进入
以下为该博客摘要:
在iOS8以前的版本中,我们使用CLLocationManager定位是没有问题的,最近在iOS8系统中却无法定位了。。。。这是一大问题啊!
iOS8中使用CoreLocation定位
1、在使用CoreLocation前需要调用如下函数【iOS8专用】:
iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法:
(1)始终允许访问位置信息
- (void)requestAlwaysA
(2)使用应用程序期间允许访问位置数据
- (void)requestWhenInUseA
示例如下:
locationManager=[[CLLocationManager alloc] init];
locationManager.delegate=
locationManager.desiredAccuracy=kCLLocationAccuracyB
locationManager.distanceFilter=10;
if (iOSVersion&=8) {
[locationManager requestWhenInUseAuthorization];//使用程序其间允许访问位置数据(iOS8定位需要)
[locationManager startUpdatingLocation];//开启定位
2、在Info.plist文件中添加如下配置:
(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription
这样添加后,定位功能就能正常使用了!问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
开启定位有两个方法,startMonitoringSignificantLocationChanges和startUpdatingLocation这两个方法有什么区别吗?
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
1、实现后台定位有2种方式:standard location service(调用CLLocationManager的startUpdatingLocation)significant-change location service(调用CLLocationManager 的startMonitoringSignificantLocationChanges)2、两者区别:前者(startUpdatingLocation)是标准定位,想要在后台使用必须在info.plist文件中增加Required background modes属性,并选择App registers for location updates值。前者(startUpdatingLocation)在后台运行时可能会因为资源问题被系统挂起(suspend)或终止(terminate),但一旦有更新会被唤起,但是当更新时系统任然资源紧张,则会被延迟调用委托。如果对于实时性要求高的可能不适合这个,很难控制用户机器性能状况。前者(startUpdatingLocation)如果被用户手动关闭,就不会再被唤醒。前者定位基于gps/基站/wifi定位,具体使用哪一种CoreLocation框架有一套自己的规则。后者(startMonitoringSignificantLocationChanges)是使用基站定位的,所以设备一定要有电话模块,在plist中可以设置xx属性来限制可被下载安装的设备。后者(startMonitoringSignificantLocationChanges)不管是在后台还是用户手动关闭都会被唤醒调用委托,只有3种方法可以阻止它的更新。(1)用户关闭定位服务(2)用户关闭对该app的定位服务(3)设备处于飞行模式或者无法开启必要的硬件(猜测是定位模块的硬件)。后者(startMonitoringSignificantLocationChanges)什么时候更新呢?是在更换基站的时候更新。所以更新频率与基站密度有关。市区更新频率较郊区高。所以很多同学说没有更新是因为还在同一组基站范围内。前者较后者耗电且精度高。3、两者共性:两者都更新位置信息时都回调相同的委托方法:-(void)locationManager:(CLLocationManager )manager didUpdateLocations:(NSArray )4、上传信息:由于后台任务只分配了有限时间执行必要的操作,所以如果在超时之前未完成(比如网络请求),app将会被终止。这里有一个方法,可以申请额外的10分钟让你执行想要的操作,申请后台任务:beginBackgroundTaskWithExpirationHandler(不详细说了,使用方法可以查一下资料)。5、总结:2种方式各有利弊,根据使用场景而决定,前台运行的app通常要求准确实时定位,并且运行时间有限,考虑用第一种标准定位,比如导航应用。如果移动速度快(距离变化明显),长时间定位(监控),可以考虑后者,比如打车应用。引用自
分享到微博?
Hi,欢迎来到 SegmentFault 技术社区!⊙▽⊙ 在这里,你可以提出编程相关的疑惑,关注感兴趣的问题,对认可的回答投赞同票;大家会帮你解决编程的问题,和你探讨技术更新,为你的回答投上赞同票。
明天提醒我
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:1.iOS8以前使用CLLocationManager
1.导入头文件 &CoreLocation/CoreLocation.h&2.创建位置管理者 CLLocationManager , 并添加到属性。3.设置代理、遵守协议、实现代理方法,在代理方法中获取位置信息4.调用开始更新位置方法
5.设置 每隔多远定位一次 和 精确度,精确度越高越耗电,定位时间越长
@property (nonatomic, strong) CLLocationManager *lcM
if ([CLLocationManager locationServicesEnabled]) {
self.lcManager = [[CLLocationManager alloc] init];
self.lcManager.delegate = self;
self.lcManager.distanceFilter = 100;
self.lcManager.desiredAccuracy = kCLLocationAccuracyB
[self.lcManager startUpdatingLocation];
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
NSLog(@&定位到了&);
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
NSLog(@&获取定位失败&);
6.请求授权,iOS6之后,苹果开始加强保护用户隐私,在 Info.plist 文件中定义 Key提醒用户,提高用户允许定位的概率。
Info.plist 设置Key
7.如果要后台定位,需要打开后台模式
勾选后台模式
2.iOS8.0之后使用CLLocationManager
1.iOS8之后,苹果又进一步加强了隐私保护,不会主动填出对话框,让用户选择2.需要实现两个方法(实现其一即可),并且 Info.plist 中设置对应的 key ,才会弹框
1.requestWhenInUseAuthorization
1.当程序当前的授权状态为未决定时,在前台时请求定位服务许可时使用。需要先在 Info.plist 文件中设置一个Key:NSLocationWhenInUseUsageDescription, 如果不设置key,系统会忽略定位请求。
Info.plist 设置对应的Key
2.当用户授权 when-in-use时,程序在前台时可以启动大部分定位服务。如果想要后台定位,需要开启后台定位模式,但在状态栏会出现蓝条提示用户程序正在进行定位。
[_lcManager requestWhenInUseAuthorization]
请求定位的弹框
后台模式下的 blue bar
2.requestAlwaysAuthorization
1.请求前后台定位服务授权,当授权状态为未决定时请求用户授权。前提是在 Info.plist 文件中包含key NSLocationAlwaysUsageDescription
Info.plist中 设置对应的Key
请求定位的弹框
1.iOS8之后,如果想要定位,必须调用 requestWhenInUseAuthorization 或 requestAlwaysAuthorization方法。2.如果两个请求授权的方法都执行了,会出现以下情况
1.when-in-use写在前面,第一次打开程序时请求授权,如果勾选了后台模式,进入后台会出现蓝条提示正在定位。当程序退出,第二次打开程序时
Always 会再次请求授权。之后进入后台就不会出现蓝条了(前后台都能定位)。2.Always写在前面, when-in-use写在后面,只会在第一次打开程序时请求授权,因为
Always得到的授权大于when-in-use的到的授权
4.判断是否开启了定位服务
在启动更新位置之前要先判断是否开启了定位服务
if ([CLLocationManager locationServicesEnabled]) {
[self.lcManager startUpdatingLocation];
5.适配版本号的方法
when-in-use和 Always 都是iOS8之后出现的方法,如果不进行版本适配,运行在iOS7上就会crash,此时需要做版本号判断1.判断版本号
if ([[UIDevice currentDevice].systemVersion floatValue] &=8.0 ) {
[_lcManager requestAlwaysAuthorization];
2.适配版本的另一种方法
if ([_lcManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[_lcManager requestWhenInUseAuthorization];
6.监听定位服务状态的改变
实现代理方法,判断定位服务的状态
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
switch (status) {
case kCLAuthorizationStatusNotDetermined:
NSLog(@&用户还未决定授权&);
case kCLAuthorizationStatusRestricted:
NSLog(@&访问受限&);
case kCLAuthorizationStatusDenied:
if ([CLLocationManager locationServicesEnabled]) {
NSLog(@&定位服务开启,被拒绝&);
NSLog(@&定位服务关闭,不可用&);
case kCLAuthorizationStatusAuthorizedAlways:
NSLog(@&获得前后台授权&);
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@&获得前台授权&);
7.代理方法返回的 locations 信息
当位置管理器,获取到位置后,调用 locationManager:didUpdateLocations:方法,返回的类型为
CLLocation 的位置信息数组,以下为数组包含的属性
1.coordinate : 当前位置的坐标
latitude : 纬度longitude : 经度
2.altitude : 海拔,高度3.horizontalAccuracy : 纬度和经度的精度4.verticalAccuracy : 垂直精度(获取不到海拔时为负数)5.course : 行进方向(真北)6.speed : 以米/秒为单位的速度7.description : 位置描述信息
()locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
CLLocation *location = [locations firstObject];
(@&%@&, location);
3.iOS9.0之后使用CLLocationManager
1.iOS9.0之后有一种新的请求定位的方法 requestLocation2.作用:按照定位精确度从低到高进行排序,逐个进行定位。如果获取到的位置不是精确度最高的那个,也会在定位超时后,通过代理告诉外界。3.注意:
1.必须实现 ocationManager:didUpdateLocations: 和 locationManager:didFailWithError 方法,但是只调用一次2.不能与startUpdatingLocation同时使用
if ([CLLocationManager locationServicesEnabled]) {
[self.lcManager requestLocation];
4.实现 requestWhenInUseAuthorization 或 requestAlwaysAuthorization 方法,并设置对应的 key
if ([[UIDevice currentDevice].systemVersion floatValue] &=8.0 ) {
[_lcManager requestWhenInUseAuthorization];
5.必须勾选后台模式,并设置 allowsBackgroundLocationUpdates 属性为YES(默认是NO)
1.当定位完成时,设置为NO,并且不再定位跟踪
2.使用 -responsdToSelector: 判断
if ([_lcManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) {
_lcManager.allowsBackgroundLocationUpdates = YES;
文/牧晓逸风(简书作者)
原文链接:/p/ef6994767cbb
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1081208次
积分:7129
积分:7129
排名:第3099名
原创:74篇
转载:262篇
评论:135条
email &&-&
weixin -& ruglcc
(1)(6)(2)(4)(2)(2)(11)(1)(20)(17)(3)(3)(7)(8)(3)(9)(17)(3)(1)(1)(10)(4)(2)(30)(122)(32)(3)(5)(3)(4)拒绝访问 |
| 百度云加速
请打开cookies.
此网站 () 的管理员禁止了您的访问。原因是您的访问包含了非浏览器特征(38ada1d007ee43dd-ua98).
重新安装浏览器,或使用别的浏览器

我要回帖

更多关于 cloudera manager使用 的文章

 

随机推荐