我有一大堆代码,我可以在iOS模拟中运行并获得连续的位置更新,但是当我在iPad上运行它时,它似乎在第三次更新后停止。我曾尝试将activityType更改为其他或设置可能导致其暂停更新的内容,但这些似乎没有帮助。我也不清楚为什么它会在现实生活中而不是在模拟中停止。
我确实尝试在每次更新后停止更新,然后是startUpdates,这似乎确实有效,但这让我觉得行为不好。有没有人对这类问题有其他建议或经验。
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *myLocationManager;
@end
@implementation ViewController
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
for (int locIndex = 0; locIndex < locations.count; locIndex++)
{
CLLocation *newLocation = [locations objectAtIndex:locIndex];
NSLog(@"Latitude = %f", newLocation.coordinate.latitude);
NSLog(@"Longitude = %f", newLocation.coordinate.longitude);
}
CLLocationDistance distance = 0;
NSTimeInterval timeout = 1;
[manager allowDeferredLocationUpdatesUntilTraveled:distance timeout:timeout];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
CLLocationDistance distance = 0;
NSTimeInterval timeout = 1;
[manager allowDeferredLocationUpdatesUntilTraveled:distance timeout:timeout];
/* We received the new location */
NSLog(@"Latitude = %f", newLocation.coordinate.latitude);
NSLog(@"Longitude = %f", newLocation.coordinate.longitude);
//[self.myLocationManager stopUpdatingLocation];
//[self.myLocationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
/* Failed to receive user's location */
NSLog(@"******************************************************");
NSLog(@"************** FAILED TO RECEIVE LOCATION ************");
NSLog(@"******************************************************");
}
- (void)viewDidLoad {
[super viewDidLoad];
if ([CLLocationManager locationServicesEnabled]){
self.myLocationManager = [[CLLocationManager alloc] init];
self.myLocationManager.delegate = self;
//self.myLocationManager.pausesLocationUpdatesAutomatically = NO;
//self.myLocationManager.activityType = CLActivityTypeOther;
[self.myLocationManager startUpdatingLocation];
//self.myPeripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
} else {
/* Location services are not enabled.
Take appropriate action: for instance, prompt the
user to enable the location services */
NSLog(@"Location services are not enabled");
}
}
答案 0 :(得分:0)
当用户忘记GPS处于活动状态时使用暂停。为了防止GPS耗尽电池,gps会在应用程序再次启动之前停止并再次打开。
您应该捕获暂停呼叫并询问用户是否应继续更新位置。