Does CLLocationManager works without internet?

纵然是瞬间 提交于 2019-12-12 09:33:27

问题


Silly question !! Though I want to know that is it possible that CLLocationManager works without Internet ?????

i.e. iPhone is not connected to internet at all


回答1:


iPhoneiPadDev's answer is slightly wrong: whilst sometimes the location manager will fail, it can work without network connectivity. If you want to see this for yourself, go for a drive somewhere with terrible or nonexistent cellular reception: GPS will still work.

It very much depends on the environmental conditions around you, and the device you're using. iPod Touches and some iPads don't have GPS, and rely on WiFi hotspots to determine their location data. If you don't have network access the CLLocationManager will return an invalid location.

iPhones and 3G iPads do have GPS, so you may get an appropriate location returned. However, they use A-GPS (assisted GPS), which uses information from the network to allow a faster GPS lock. If you don't have internet connectivity it may take some time for the GPS chip to obtain a signal and provide an accurate location: and accuracy may be wildly off, if you're indoors or don't have plain sight of the sky.

Important point: CLLocationManager can and will return you locations even if none are available: the coordinates, however, will be invalid. It's important to test the locations being returned and make sure you're satisfied they are correct before using them.




回答2:


Yeah it works if Location Services are enabled in the terminal's settings. No need for Internet connection.




回答3:


Yes but in open sky(Means on road, ground).

  1. If you are online, you will get location coordinate from wifi, mobile data then if you switch off internet and change your location(Away from that location), Say you are now in building(3rd floor) then you won't get correct location update instead you will get cache coordinates.

To fix this:

Record timestamp(Say "RecordDate") in offline mode when you want location then compare timestamp given by didUpdateToLocations: method.

if didUpdateToLocations's timestamp > Recorded Timestamp then use it

- (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;
    }
}


来源:https://stackoverflow.com/questions/7346794/does-cllocationmanager-works-without-internet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!