startUpdatingLocation 仅在 wifi 开启时有效,但仅在 3G 开启时无效
Posted
技术标签:
【中文标题】startUpdatingLocation 仅在 wifi 开启时有效,但仅在 3G 开启时无效【英文标题】:startUpdatingLocation is working only when wifi is ON but not when only 3G in ON 【发布时间】:2015-03-12 08:17:04 【问题描述】:我试图获取用户当前位置的经度/纬度,我的代码就是这样:
MyLocationManager.h
@interface MyLocationManager : NSObject
+(MyLocationManager*)sharedInstance;
-(void)checkAndStartLocationManager;
-(void)stopUpdatingLocation;
@end
MyLocationManager.m
+ (MyLocationManager*)sharedInstance
static id _sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^
_sharedInstance = [[self alloc] init];
MyLocationManager *instance = _sharedInstance;
instance.locationManager = [CLLocationManager new];
instance.locationManager.delegate = instance;
instance.locationManager.distanceFilter = 100;
instance.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
instance.locationManager.pausesLocationUpdatesAutomatically = YES;
instance.locationManager.activityType = CLActivityTypeOtherNavigation;
);
return _sharedInstance;
-(void)checkAndStartLocationManager
switch([CLLocationManager authorizationStatus])
case kCLAuthorizationStatusAuthorizedWhenInUse:
[self startUpdatingLocation];
break;
case kCLAuthorizationStatusNotDetermined:
if ([self.locationManager respondsToSelector:
@selector(requestWhenInUseAuthorization)])
[self.locationManager requestWhenInUseAuthorization];
break;
case kCLAuthorizationStatusRestricted:
break;
case kCLAuthorizationStatusDenied:
break;
case kCLAuthorizationStatusAuthorizedAlways:
[self startUpdatingLocation];
break;
default:
break;
-(void)startUpdatingLocation
[self.locationManager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status
switch([CLLocationManager authorizationStatus])
case kCLAuthorizationStatusAuthorizedWhenInUse:
[self startUpdatingLocation];
break;
case kCLAuthorizationStatusNotDetermined:
break;
case kCLAuthorizationStatusRestricted:
break;
case kCLAuthorizationStatusDenied:
break;
case kCLAuthorizationStatusAuthorizedAlways:
[self startUpdatingLocation];
break;
default:
break;
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations: (NSArray *)locations
CLLocation *location = [locations lastObject];
NSString*locationInfo = [NSString stringWithFormat:@"lat - %f, lon - %f", location.coordinate.latitude, location.coordinate.longitude];
NSLog(@"Location is %@", locationInfo);
[self.locationManager stopUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didFailWithError: (NSError *)error
NSLog(@"didFailWithError: %@", error);
我正在使用 ios 8 并在 iphone 5 设备上进行了测试。每当我打电话时,
[[MyLocationManager sharedInstance] checkAndStartLocationManager];
如果手机上的wifi是开的,那么只有
didUpdateLocations
正在被调用。 如果 wifi 处于关闭状态并且只有 3G 处于打开状态,那么我 总是收到错误,
Error Domain=kCLErrorDomain Code=0 "操作无法完成。(kCLErrorDomain error 0.)"
请帮忙!!!
【问题讨论】:
【参考方案1】:设备使用 WiFi 通过众包热点快速确定位置,尽管结果可能不准确。当您禁用 WiFi 时,唯一剩下的可能性就是使用 GPS 模块。但是,如果您在某些建筑物中或 GPS 信号不够强,则需要更多时间来确定您的位置,并且您会在一段时间内收到该消息
【讨论】:
谢谢阿扎特。你是对的,在某些建筑物中或 GPS 信号较弱时需要更多时间。以上是关于startUpdatingLocation 仅在 wifi 开启时有效,但仅在 3G 开启时无效的主要内容,如果未能解决你的问题,请参考以下文章
同时使用 startUpdatingLocation 和 startMonitoringSignificantChanges?
startUpdatingLocation 不再在 XCode 6.1 中工作
StartUpdatingLocation 与重大变化的位置服务
iOS startUpdatingLocation 从未调用过
找不到 CLLocationManager startUpdatingLocation
CLLocationManager startUpdatingLocation 与 startMonitoringSignificantLocationChanges