CLLocationManager没有互联网吗?

问题描述 投票:13回答:3

愚蠢的问题 !!虽然我想知道CLLocationManager可以在没有Internet的情况下工作吗?????

即iPhone根本没有连接到互联网

iphone ipad cllocationmanager
3个回答
13
投票

iPhoneiPadDev的答案有点错误:虽然有时位置管理器会失败,但它可以在没有网络连接的情况下工作。如果你想亲眼看到这个,那么在某个地方有一个可怕或不存在蜂窝接收的驱动器:GPS仍然有用。

这在很大程度上取决于您周围的环境条件以及您正在使用的设备。 iPod Touches和一些iPad没有GPS,并依靠WiFi热点来确定他们的位置数据。如果您没有网络访问权限,CLLocationManager将返回无效的位置。

iPhone和3G iPad确实有GPS,因此您可以获得适当的位置。然而,他们使用A-GPS(辅助GPS),它使用来自网络的信息来实现更快的GPS锁定。如果您没有互联网连接,那么GPS芯片可能需要一些时间才能获得信号并提供准确的位置:如果您在室内或者看不到天空,则精确度可能会大幅下降。

重点:CLLocationManager可以并且将返回您的位置,即使没有可用的位置:但坐标将无效。测试返回的位置非常重要,并确保在使用之前确定它们是正确的。


4
投票

是的,如果在终端的设置中启用了位置服务,它是否有效。无需互联网连接。


0
投票

是的,但在开阔的天空(道路,地面上的手段)。

  1. 如果您在线,您将获得来自wifi,移动数据的位置坐标,然后如果您关闭互联网并更改您的位置(远离该位置),假设您现在在建筑物(3楼),那么您将无法获得正确的位置更新,您将获得缓存坐标。

解决这个问题:

当您想要位置时记录时间戳(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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.