开发者

Does CLLocationManager work without internet/cellular connection?

开发者 https://www.devze.com 2023-04-03 06:51 出处:网络
Silly question !! Though I want to know that is it possible that CLLocationManager works without Internet ?????

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


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.


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


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


Core Location uses a number of sensors to get a location: Bluetooth, Wi-Fi, GPS, barometer, magnetometer, and what Apple calls "cellular hardware". GPS, BLE, barometer, and magnetometer all don't need a data connection.

0

精彩评论

暂无评论...
验证码 换一张
取 消