在iOS中的固定点和我当前位置之间行走时计算距离

时间:2018-07-02 12:26:25

标签: ios core-location cllocationmanager

我必须计算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];
}

1 个答案:

答案 0 :(得分:0)

您可以使用Haversine公式计算两点之间的距离

swift 3.x

var el = document.getElementById('table');
var dragger = tableDragger(el, {
  mode: 'column',
  dragHandler: '.handle',
  onlyBody: true,
  animation: 300
});

dragger.on('drop',function(from, to){
  // Setting the Value.
  localStorage.setItem("keyName", "value"); // Syntax
  localStorage.setItem("table_layout", table_layout); // by variable
  localStorage.setItem("table_layout", "from to?"); // by value

   // Get the value.
   localStorage.getItem("table_layout");

   // Clear one item.
   localStorage.removeItem("table_layout");

   // Clear all items.
   localStorage.clear();
});