1

I am calculating the distance between two locations like below

             CLLocation *locA = [[CLLocation alloc] initWithLatitude:lat longitude:lon];

            CLLocationCoordinate2D coordinate = [self getLocation];

            CLLocation *locB = [[CLLocation alloc] initWithLatitude:coordinate.latitude   longitude:coordinate.longitude];

            CLLocationDistance distance = [locA distanceFromLocation:locB];


            CLLocationDistance kilometers = distance / 1000.0;

But it giving the different result when i connect to wifi and 3G network.how to get the same result for both.

venkat
  • 345
  • 8
  • 21

2 Answers2

2

You invariably will not get the same location for both. In fact, if you come back to the exact same location tomorrow and turn on location services, you might not get the same latitude and longitude even if you stayed with the same technology.

The latitude and longitude you receive from a CLLocationManager is not an exact science. In fact, if you look at the original CLLocation objects you received, you'll notice that there is a horizontalAccuracy property that reflects how accurate/inaccurate the coordinate could be. When you first start location services, you'll even see multiple locations come in, generally with increasing accuracy.

But it will never be perfect and you shouldn't expect it to be. This is one of the reasons why it's important to test location-aware code on a real device, because the simulator will lull one into a false sense of absolute accuracy which doesn't exist in the real world. You should write code that anticipates this. And, if nothing else, you should start considering horizontalAccuracy of the objects you receive from the location manager.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Is it not true that accuracy of location varies with network and way it identifies the location. Is it not true that wifi is the most accurate with its result? – Girish Kolari Feb 18 '15 at 06:26
  • 1
    Yes, the available hardware (combination of GPS, cellular towers, and/or wifi) will affect the results you get. But it is not simply a question of accuracy, but rather one of the speed to achieve increasing accurate locations. Turn on location services and you'll generally receive a series of increasingly more accurate location updates (even if you're not moving). But wifi can generally get a reasonably accurate location more quickly. – Rob Feb 18 '15 at 12:53
  • See http://www.macworld.com/article/1159528/how_iphone_location_works.html for discussion. – Rob Feb 18 '15 at 12:54
0

there's nothing wrong in the code lines you posted, the only line that could give you differences in the 2 cases (wifi and 3g) is your method [self getLocation]

where i guess you get the device location.

In this case your device may use both gps and wifi access to calculate its location, so little differences may occurs (you may have noticed that some map applications advice you to turn your wifi on to get more accuracy)

Anyway, in your getLocation method try to add a more precise accuracy when you use CLLocationManager

i tried this:

- (CLLocationCoordinate2D )getLocation {
    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
    [locationManager startUpdatingLocation];
    CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude);

    return startCoord;
}

and the accuracy is enough to get no differences between 3G/wifi but you could set it to

kCLLocationAccuracyBestForNavigation;
kCLLocationAccuracyBest;
kCLLocationAccuracyNearestTenMeters;

ps i hope you are using a device with GPS, of course...

meronix
  • 6,175
  • 1
  • 23
  • 36