-3

The code:

CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([[[NSUserDefaults standardUserDefaults] objectForKey:@"lat"] floatValue], [[[NSUserDefaults standardUserDefaults] objectForKey:@"lon"] floatValue]);
CLLocation *currentLocation = [[CLLocation alloc] initWithCoordinate:coordinate altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];
[objects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([self.photos[idx][@"lat"] floatValue], [self.photos[idx][@"lon"] floatValue]);
    CLLocation *location = [[CLLocation alloc] initWithCoordinate:coordinate altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];
    CLLocationDistance distance = [location distanceFromLocation:currentLocation]/1000;
}];

If I NSLog the currentLocation and the location I get the same coordinate, because the are the same with minor differences. The locations are: lat: 37.331581 lon: -122.030708 and the current location are: "lat": "37.33154", "lon": "-122.0307". As you can see if I get the distance from these two locations, I should get 0 km. But I am getting 12781.989811. Why is that? I am running the simulator. If that's the case, or my codes are wrong?

yong ho
  • 3,892
  • 9
  • 40
  • 81
  • Nah, the method returns `0.004605` for the given coordinates. – A-Live Nov 06 '13 at 13:59
  • How are you logging distance and the coordinates? The behavior of the distanceFromLocation method itself is the same regardless of whether running in the simulator or device. The result depends on the coordinates of the locations only. Unrelated but you should be using doubleValue instead of floatValue for greater accuracy (and since CLLocationDegrees is a double). –  Nov 06 '13 at 14:11
  • Is it possible that because I use enumerate method, I didn't get what I expected? – yong ho Nov 08 '13 at 01:51

1 Answers1

1

Reduce the problem, that is make it as simple as possible that has the same problem.
Post code that someone trying to answer can run.
We do not have your userDefauts, that makes the problem non-reproducible.
Add NSLog statements that show the problem.

I suspect that if you do these things you will solve your problem yourself.

Example of how to present the question:

CLLocationCoordinate2D coordinate1 = CLLocationCoordinate2DMake(37.331581, -122.030708);
CLLocation *location1 = [[CLLocation alloc] initWithCoordinate:coordinate1 altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];

CLLocationCoordinate2D coordinate2 = CLLocationCoordinate2DMake(37.331581, -122.030708);
CLLocation *location2 = [[CLLocation alloc] initWithCoordinate:coordinate2 altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];

CLLocationDistance distance1 = [location1 distanceFromLocation:location2]/1000;
NSLog(@"distance: %f", distance1);

CLLocationCoordinate2D coordinate3 = CLLocationCoordinate2DMake(37.33154, -122.0307);
CLLocation *location3 = [[CLLocation alloc] initWithCoordinate:coordinate3 altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];

CLLocationDistance distance2 = [location1 distanceFromLocation:location3]/1000;
NSLog(@"distance: %f", distance2);

NSLog output:

2013-11-06 09:28:17.661 Test-iOS[45090:70b] distance: 0.000000
2013-11-06 09:28:17.663 Test-iOS[45090:70b] distance: 0.004605

Oh, there is no error!

Best guess is the user defaults are not what you expect or the input to the enumeration is not what you expect.

zaph
  • 111,848
  • 21
  • 189
  • 228