【发布时间】:2014-03-08 20:44:10
【问题描述】:
我有一段代码可以在 iOS 模拟中运行并获得连续的位置更新,但是当我在 iPad 上运行它时,它似乎在第三次更新后停止。我试图将 activityType 更改为 other or 并设置可能导致它暂停更新的内容,但这些似乎没有帮助。我也不清楚为什么它在现实生活中而不是在模拟中停止。
我确实尝试在每次更新后停止更新,然后执行 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");
}
}
【问题讨论】:
-
iPad 是否有 SIM 卡,即是否有移动数据?
标签: ios7 core-location cllocationmanager