【发布时间】:2018-12-10 16:24:39
【问题描述】:
我必须计算 iOS 和目标 c 中两个位置之间的距离。我的一个位置固定在一个点上,当我走路时,我必须计算我当前位置和固定点之间的距离。我使用了 distanceFromLocation 方法,但我没有得到更近的距离值。我浏览了几篇文章和一些 StackOverflow 解决方案,但没有一个给出正确的结果。
下面是我使用的代码,代码中的 currentLocation 和 destinationLocation 是保存位置纬度和经度的属性。 destinationLocation 始终是一个固定位置,并且当我走路时 currentLocation 不断变化。很少有 UILabel 可以打印当前和固定的纬度和经度值。我必须每次计算这两点之间的距离。当我移动半米或更少米时,它会显示很长的距离,这也是不一致的。你能帮我看看我在这里犯了什么错误吗?有没有其他方法可以实现这一目标?谢谢!
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
CLLocation *newLocation = locations.lastObject;
self.currentLocation = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
if(self.destinationLocation == nil){
self.destinationLocation = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
self.destinationLatitude.text = [NSString stringWithFormat:@"%.8f", self.destinationLocation.coordinate.latitude];
self.destinationLongitude.text = [NSString stringWithFormat:@"%.8f", self.destinationLocation.coordinate.longitude];
}
self.currentLatitude.text = [NSString stringWithFormat:@"%.8f", self.currentLocation.coordinate.latitude];
self.currentLongitude.text = [NSString stringWithFormat:@"%.8f", self.currentLocation.coordinate.longitude];
CLLocationDistance distance = [self.currentLocation distanceFromLocation:self.destinationLocation];
self.gpsDistanceMeasurement.text = [NSString stringWithFormat:@"%.2f m", distance];
}
【问题讨论】:
-
为了获得更好的结果,您应该设置:locationManager?.desiredAccuracy = kCLLocationAccuracyBestForNavigation (swift)
-
@ΒασίληςΔ。我已经尝试过使用 kCLLocationAccuracyBestForNavigation 和 kCLLocationAccuracyBest ,但得到了相似的结果。当我远离固定点时,它至少显示出增加的距离值,但是当我靠近时,它甚至不会随着无线电的增加而减少距离。
标签: ios core-location cllocationmanager