Clocationmanager Didupdatelocation 委托在没有互联网的情况下不会被调用
Cllocationmanager Didupdatelocation delegate not getting called without internet
我目前正在开发基于位置的 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];
}
您可以使用 iPhone 的基本 GPS,它也可以在没有互联网的情况下工作。要访问它,您需要使用 CoreLocation 框架。
您可以参考:CoreLocation methods使用GPS数据。
如果需要任何其他帮助,请告诉我。
@ANSHAD 根据您的评论,如果您使用的是没有 GPS 的旧 iPad,则获取位置数据的唯一方法是使用 WiFi,因此如果您关闭 WiFi,您将无法获得位置更新。如果你想要没有 WiFi 的 GPS,你可以购买一个外部 GPS 模块 see here
我目前正在开发基于位置的 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];
}
您可以使用 iPhone 的基本 GPS,它也可以在没有互联网的情况下工作。要访问它,您需要使用 CoreLocation 框架。
您可以参考:CoreLocation methods使用GPS数据。
如果需要任何其他帮助,请告诉我。
@ANSHAD 根据您的评论,如果您使用的是没有 GPS 的旧 iPad,则获取位置数据的唯一方法是使用 WiFi,因此如果您关闭 WiFi,您将无法获得位置更新。如果你想要没有 WiFi 的 GPS,你可以购买一个外部 GPS 模块 see here