laverick
laverick

Reputation: 734

CLLocation distanceFromLocation large value

I'm having some issues with CLLocation distanceFromLocation. I have the code below, formatted to help with debugging, explicitly allocating two new locations etc. Either way, two nearly identical locations result in a huge gap, but only sometimes.

Code:

CLLocation *loc = [LightweightLocationTracker sharedLocationTracker].lastLocation;
if (loc)
{
  CLLocation *myLoc = [[CLLocation alloc] initWithLatitude:loc.coordinate.latitude longitude:loc.coordinate.longitude];
  CLLocation *productLoc = [[CLLocation alloc] initWithLatitude:self.productToView.latitude longitude:self.productToView.longitude];        
  CLLocationDistance dist = [myLoc distanceFromLocation:productLoc];

  NSLog(@"myLoc.coordinate.latitude %f", myLoc.coordinate.latitude);        
  NSLog(@"myLoc.coordinate.longitude %f", myLoc.coordinate.longitude);                
  NSLog(@"productLoc.coordinate.latitude %f", productLoc.coordinate.latitude);
  NSLog(@"productLoc.coordinate.longitude %f", productLoc.coordinate.longitude);
  NSLog(@"dist %f", dist);        
  NSLog(@"%1.1f km", dist);
}

One run TTY:

[] tapped main
[] myLoc.coordinate.latitude 52.369200
[] myLoc.coordinate.longitude 13.941100
[] productLoc.coordinate.latitude 52.364400
[] productLoc.coordinate.longitude 13.360600
[] dist 39542.386770
[] 39542.4 km

Another run TTY:

[] myLoc.coordinate.latitude 52.369200
[] myLoc.coordinate.longitude 13.941100
[] productLoc.coordinate.latitude 52.369202
[] productLoc.coordinate.longitude 13.941100
[] dist 0.184915
[] 0.2 km

Not seeing the culprit...

Upvotes: 1

Views: 1405

Answers (1)

danqing
danqing

Reputation: 3668

Well, distanceFromLocation returns meters. So that 30k thing is just 30km. Sounds perfectly legit to me.

Upvotes: 3

Related Questions