我在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

没有看到罪魁祸首...

最佳答案

好吧,distanceFromLocation返回米。所以30k的东西只有30km。对我来说听起来很合法。

关于ios - CLLocation distanceFromLocation大值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11786722/

10-09 02:34