愚蠢的问题 !!虽然我想知道CLLocationManager可以在没有Internet的情况下工作吗?????
即iPhone根本没有连接到互联网
iPhoneiPadDev的答案有点错误:虽然有时位置管理器会失败,但它可以在没有网络连接的情况下工作。如果你想亲眼看到这个,那么在某个地方有一个可怕或不存在蜂窝接收的驱动器:GPS仍然有用。
这在很大程度上取决于您周围的环境条件以及您正在使用的设备。 iPod Touches和一些iPad没有GPS,并依靠WiFi热点来确定他们的位置数据。如果您没有网络访问权限,CLLocationManager
将返回无效的位置。
iPhone和3G iPad确实有GPS,因此您可以获得适当的位置。然而,他们使用A-GPS(辅助GPS),它使用来自网络的信息来实现更快的GPS锁定。如果您没有互联网连接,那么GPS芯片可能需要一些时间才能获得信号并提供准确的位置:如果您在室内或者看不到天空,则精确度可能会大幅下降。
重点:CLLocationManager
可以并且将返回您的位置,即使没有可用的位置:但坐标将无效。测试返回的位置非常重要,并确保在使用之前确定它们是正确的。
是的,如果在终端的设置中启用了位置服务,它是否有效。无需互联网连接。
是的,但在开阔的天空(道路,地面上的手段)。
解决这个问题:
当您想要位置时记录时间戳(Say“RecordDate”),然后比较didUpdateToLocations:method给出的时间戳。
如果didUpdateToLocations的时间戳> Recorded Timestamp然后使用它
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations
{
CLLocation *currentLocation = [locations lastObject];
NSLog(@"didUpdateToLocation: %@", currentLocation);
NSDate *eventDate=[[NSUserDefaults standardUserDefaults] objectForKey:@"RecordDate"];
NSComparisonResult result = [currentLocation.timestamp compare:eventDate];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
switch (result)
{
case NSOrderedAscending: {
NSLog(@"Location%@ is greater than %@", [df stringFromDate:eventDate], [df stringFromDate:currentLocation.timestamp]);
}
break;
case NSOrderedDescending:
NSLog(@"%@ is less %@", [df stringFromDate:eventDate], [df stringFromDate:currentLocation.timestamp]);
if (currentLocation != nil)
{
//Use them
//currentLocation.coordinate.latitude
// currentLocation.coordinate.longitude
}
break;
case NSOrderedSame:
//Use them
break;
default:
NSLog(@"erorr dates %@, %@", [df stringFromDate:eventDate], [df stringFromDate:currentLocation.timestamp]);
break;
}
}