CLLocation distanceFromLocation值很大

时间:2012-08-02 22:24:29

标签: ios cllocationmanager

我在使用CLLocation distanceFromLocation时遇到了一些问题。我有下面的代码,格式化以帮助调试,明确分配两个新位置等。无论哪种方式,两个几乎相同的位置导致巨大的差距,但只是有时。

代码:

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

一次运行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

另一个运行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

没有看到罪魁祸首......

1 个答案:

答案 0 :(得分:3)

好吧,distanceFromLocation返回米。所以30k的东西只有30km。听起来非常合法。