我目前正在开发基于位置的ios应用程序。我正在使用didupdatelocation
委托方法来显示用户的当前位置。当我将设备连接到互联网时,它可以正常工作。但是当我断开互联网连接时,它的行为很奇怪,而且didupdatelocation
不再被调用。请给出一个解决方案。
已编辑,包含代码详细信息
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc]init]; // initializing locationManager
locationManager.delegate = self; // we set the delegate of locationManager to self.
locationManager.desiredAccuracy = kCLLocationAccuracyBest; // setting the accuracy
[locationManager startUpdatingLocation]; //requesting location updates
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"There was an error retrieving your location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[errorAlert show];
NSLog(@"Error: %@",error.description);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *crnLoc = [locations lastObject];
latitude.text = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.latitude];
longitude.text = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.longitude];
altitude.text = [NSString stringWithFormat:@"%.0f m",crnLoc.altitude];
speed.text = [NSString stringWithFormat:@"%.1f m/s", crnLoc.speed];
}
最佳答案
根据您的评论@ANSHAD,如果您使用的是不带GPS的旧iPad,则获取位置数据的唯一方法就是使用WiFi,因此,如果您关闭WiFi,则不会获得位置更新。如果您想要没有WiFi的GPS,则可以购买外部GPS模块see here