所以当我连接到 WiFi 时它可以工作。但是在 4G 上,它只有在我使用 Wifi 并且它已经有位置的情况下才有效。很多时候没有 WiFi,手机会说它正在使用我的位置,但它不会更新标签,也不会将坐标上传到服务器。这是代码:
- (void)viewDidLoad {
[super viewDidLoad];
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([self->locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self->locationManager requestWhenInUseAuthorization];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *newLocation = [locations lastObject];
NSString *locationLat = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.latitude];
NSString *locationLong = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.longitude];
latLongLabel.text = [NSString stringWithFormat:@"Lat: %@ - Long%@",locationLat,locationLong];
[self postLocation:locationLat secondArg:locationLong];
}
- (void)postLocation: (NSString *)latitudeString secondArg:(NSString *)longitudeString {
//POST COORDINATES TO MY SERVER
}
- (IBAction)startUpdating:(id)sender {
[locationManager startUpdatingLocation];
}
最佳答案
我怀疑当你使用 4G 时,位置更新仍然可以正常工作(虽然 wifi 三角测量使它们更精确但只有 Apple 知道如何实现,因为实现是私有(private)的),但是发送那些可能有问题通过 4G 连接快速或可靠地向服务器发送值。 (例如在伦敦,周围有很多人,速度很慢)
您可以通过简单地将位置更新直接记录到 View 上的某个 UIlabel 来缩小调试范围,而不是通过服务器基础设施。
作为最后的手段,我会确保
您将 CLActivityType
属性设置为 CLActivityTypeFitness
并且 pausesLocationUpdatesAutomatically
设置为 NO
。
关于iOS 8 Core Location 每次都在 WiFi 上工作,在 4G 上时断时续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28224764/