4

I am using CLLocation to work out the distance from the current user location, and an annotation. However I just wanted to know if this would be correct. I am currently using iPhone Simulator for this and according to the MKMapView the iPhone Simulator is situated here:

Lat: 0 Long: -1067024384

The annotation's position is:

workingCoordinate.latitude = 40.763856;
workingCoordinate.longitude = -73.973034;

However if you take a look in google maps you will find out how close these distances are, yet so far apart according to CLLocation. I am using the following code to determine the distance between them both.

CLLocation *loc = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];
CLLocationDistance dist = [loc distanceFromLocation:loc2];
int distance = dist
NSLog(@"%i", distance);

The distance being NSLogged is 12769908. I believe that this is incorrect, and therefore there must be a problem with my code.

If there is please can you point it out!

max_
  • 24,076
  • 39
  • 122
  • 211

2 Answers2

9

You have two bad habits.

  1. You should not depend on simulator in situations need hardware censor status. Especially when you want correct test.
  2. You're handling types incorrectly. So you can't check values correctly. How is the longitude -1067024384? Longitude value is degrees. This means it's valid range is limited -90.0 ~ +90.0 by definition of longitude.

Your longitude value is out of range. This means one of these. You printed the value wrongly or the real value was wrong. Simulator can print wrong value. Or you printed the value with wrong method. You have to try:

Test on real device which has real hardware censors.

If bad result continues after that,

Review ALL of your application code. Especially for printing, handling values. Check you're using correct types and castings in > each situations. Because you may did buggy operation in somewhere habitually.

And also, I recommend checking all of intermediate values like this.

CLLocationCoordinate2D annocoord = annotation.coordinate;
CLLocationCoordinate2D usercoord = self.mapView.userLocation.coordinate;

NSLog(@"ANNO  = %f, %f", annocoord.latitude, annocoord.longitude);
NSLog(@"USER = %f, %f", usercoord.latitude, usercoord.longitude);

CLLocation *loc = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];

NSLog(@"LOC  = %f, %f", loc.coordinate.latitude,  loc.coordinate.longitude);
NSLog(@"LOC2 = %f, %f", loc2.coordinate.latitude, loc2.coordinate.longitude);

CLLocationDistance dist = [loc distanceFromLocation:loc2];

NSLog(@"DIST: %f", dist); // Wrong formatting may show wrong value!
eonil
  • 83,476
  • 81
  • 317
  • 516
  • I get the same bug on the device. here is the output of the code you provided: `ANNO = 37.331741, -122.030564 USER = -180.000000, -180.000000 LOC = 37.331741,LOC2 = -180.000000, -180.000000 LOC2 = -180.000000, -180.000000 DIST: 12769908.162754` – max_ Mar 05 '11 at 00:32
  • I am still getting the random locations as I did before. – max_ Mar 05 '11 at 01:19
1

Try @"%f" and don't cast it that way.

In CLLocation.h

typedef double CLLocationDistance;
bioffe
  • 6,283
  • 3
  • 50
  • 65
  • that hasn't made a difference. I have changed the int to a float, and it returns the same result in the console. I also tried changing it to a double. – max_ Mar 04 '11 at 20:45
  • 12769908 = 12769km and 908 meters... btw your simulator shows very bizarre location. – bioffe Mar 04 '11 at 20:48
  • may I ask how you worked that out? How can it equal 12769km? but 908 metres sounds more realistic – max_ Mar 04 '11 at 20:50
  • 12769908m = 12769km and 908 meters. 'distanceFromLocation:' function returns distance in meters. – bioffe Mar 04 '11 at 20:53
  • @XcodeDev when you mismatch printf datatypes, the output can be completely off. Can you tell what it displays as output once you convert it to doubles. BTW my default location in simulator looks like this `newLocation=<+40.71815700, -74.03419350> +/- 150.00m (speed 0.00 mps / course -1.00)` – bioffe Mar 04 '11 at 21:05
  • hey, I found the reason why the annotation's distance was wrong. However I ran the app on my itouch, and the NSLogged distance is incorrect, it is displaying -180.000000 and -180.000000: `CLLocationCoordinate2D usrloc = self.mapView.userLocation.coordinate; NSLog(@"Long: %f Lat: %f USRLOC", usrloc.longitude, usrloc.latitude); NSLog(@"Long: %f Lat: %f ANNOTATION", appleStore2.coordinate.longitude, appleStore2.coordinate.latitude); //correct` – max_ Mar 04 '11 at 21:15