从LatLng列表计算总距离 [英] Total distance calculation from LatLng List

查看:84
本文介绍了从LatLng列表计算总距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用dart / flutter和'package:latlong / latlong.dart'将GPX文件解析为LatLng对象列表。很好,但是下一步是找到路线的总距离。

Im using dart/flutter and the 'package:latlong/latlong.dart' to parse a GPX file into a list of LatLng objects. That is working fine, but the next step is to find the total distance of the route.

这里的问题是


如何从
Dart中的LatLng对象列表中获得总距离?

how can I get the total distance from a list of LatLng objects in Dart?

我以前在Java中使用过Haversine公式,但不确定使用Dart实现它的最佳方法。

I have used Haversine formula before in Java, but not sure the best way to implement it using Dart. Any help of suggestion would be greatly appreciated.

编辑

下面是一个尝试根据以下答案获得总距离。由于某些原因,它不会加到 totalDistance 中,并且不会返回双精度值。

Below is an attempt to get total distance based on the answer below. For some reason though it doesn't add to totalDistance and doesn't return a double.

下面是假设要遍历60个以上的LatLng对象的列表并找到每个对象之间的距离,并添加一个totalDistance double,该double将在循环结束时返回。

The below is suppose to iterate through a list of 60+ LatLng objects and find distance between each one, adding to a totalDistance double which is returned at the end of the loop.

 static double getDistanceFromGPSPointsInRoute(List<LatLng> gpsList) {
    double totalDistance = 0.0;

    for (var i = 0; i < gpsList.length; i++) {
      var p = 0.017453292519943295;
      var c = cos;
      var a = 0.5 -
          c((gpsList[i + 1].latitude - gpsList[i].latitude) * p) / 2 +
          c(gpsList[i].latitude * p) *
              c(gpsList[i + 1].latitude * p) *
              (1 - c((gpsList[i + 1].longitude - gpsList[i].longitude) * p)) /
              2;
      double distance = 12742 * asin(sqrt(a));
      totalDistance += distance;
      print('Distance is ${12742 * asin(sqrt(a))}');
    }
    print('Total distance is $totalDistance');
    return totalDistance;
  }

输出

flutter: Distance is 0.004143962775784678
flutter: Distance is 0.0041439635323316775
flutter: Distance is 0.007796918986828574
flutter: Distance is 0.007285385943437824
flutter: Distance is 0.006890844300938902
flutter: Distance is 0.006353952460010352
flutter: Distance is 0.005560051252981138

如您在上面看到的,这里没有提及总距离。

As you can see above, there is no mention of Total Distance.

LatLng列表示例

flutter: -36.84975 , 174.646685
flutter: -36.849692 , 174.646497
flutter: -36.84967 , 174.646436
flutter: -36.849578 , 174.646264
flutter: -36.849502 , 174.646164
flutter: -36.849367 , 174.646038
flutter: -36.849209 , 174.645959
flutter: -36.849155 , 174.64594
flutter: -36.849107 , 174.645932
flutter: -36.849058 , 174.645922
flutter: -36.848952 , 174.645895
flutter: -36.84886 , 174.645906
flutter: -36.84879 , 174.645913
flutter: -36.848748 , 174.645836
flutter: -36.848744 , 174.645802


推荐答案

请尝试一下。我使用Google Maps进行了测试,并且可以正常工作。您可以进行循环并通过每次使用2个点来找到总距离。 我添加了一些随机虚拟数据以演示其工作原理。将此代码复制到 https:// dartpad .dartlang.org / 并轻松进行测试。

Try this please. I tested it with Google Maps and works accurately. You can do a loop and find total distance by using 2 points each time. I added some random dummy data to show how it works. Copy this code to https://dartpad.dartlang.org/ and test easily.

import 'dart:math' show cos, sqrt, asin;

void main() {
  double calculateDistance(lat1, lon1, lat2, lon2){
    var p = 0.017453292519943295;
    var c = cos;
    var a = 0.5 - c((lat2 - lat1) * p)/2 + 
          c(lat1 * p) * c(lat2 * p) * 
          (1 - c((lon2 - lon1) * p))/2;
    return 12742 * asin(sqrt(a));
  }

  List<dynamic> data = [
    {
      "lat": 44.968046,
      "lng": -94.420307
    },{
      "lat": 44.33328,
      "lng": -89.132008
    },{
      "lat": 33.755787,
      "lng": -116.359998
    },{
      "lat": 33.844843,
      "lng": -116.54911
    },{
      "lat": 44.92057,
      "lng": -93.44786
    },{
      "lat": 44.240309,
      "lng": -91.493619
    },{
      "lat": 44.968041,
      "lng": -94.419696
    },{
      "lat": 44.333304,
      "lng": -89.132027
    },{
      "lat": 33.755783,
      "lng": -116.360066
    },{
      "lat": 33.844847,
      "lng": -116.549069
    },
  ];
  double totalDistance = 0;
  for(var i = 0; i < data.length-1; i++){
    totalDistance += calculateDistance(data[i]["lat"], data[i]["lng"], data[i+1]["lat"], data[i+1]["lng"]);
  }
  print(totalDistance);
}

这篇关于从LatLng列表计算总距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆