代码之家  ›  专栏  ›  技术社区  ›  BIS Tech

出行距离的计算方法

  •  0
  • BIS Tech  · 技术社区  · 5 年前

    我正在为我们的司机创建一个行程计算应用程序。上述方法正在发挥作用。但一定要计算距离。

    为什么总是在不移动手机的情况下返回不同的板条?(我桌上的手机)

    输出

    在不移动设备的情况下,我在10分钟内获得了超过10公里的成绩。为什么?

    double calculate(){
    double cal = 0;
    
    list.removeWhere((element) => element?.latitude == userLocation?.latitude || element?.longitude == userLocation?.longitude); //remove duplicate values
    list.add(userLocation);
              
    if (list.length > 1) {
       LocationModel firstLoc = list.elementAt(list.length - 2);
       LocationModel lastLoc = list.last;
    
       cal = _cp.calculateDistance(firstLoc.latitude, firstLoc.longitude, lastLoc.latitude, lastLoc.longitude);
    }
    
     totalKm += cal;
     retun totalKm;
    }
    
    
      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));
      }
    

    GPS服务方法

    我在用 geolocator 插件。( FusedLocationProviderClient 或者,如果不可用 LocationManager 关于Android和 CLLocationManager 在iOS上)。

      Future<LocationModel> getLocation() async {
        try {
          Position userLocation = await Geolocator.getCurrentPosition(forceAndroidLocationManager: false, desiredAccuracy: LocationAccuracy.high);
          _currentLocation = LocationModel.fromGeoLocatorData(userLocation);
        } on Exception catch (e) {
          print('Could not get location: $e');
        }
        return _currentLocation;
      }
    
    Timer.periodic(Duration(seconds: 1), (Timer t) async {
          userLocation = await locationService.getLocation().then(getLocation);
        });
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   Ridcully    5 年前

    移动设备的位置跟踪不是很确定。即使你站着不动,当你投票的时候,每一次测量都会有一点变化。此外,你还应该考虑位置的准确性。基本上,你得到的位置并不意味着这个位置正好是lat/long。这实际上意味着,在68%的置信度下,位置在lat/long周围的精度米半径范围内。[1].

    因此,如果两个位置之间的距离大于两个精度之和,则只应假设两个位置不同。只有当您确定新位置与前一个位置不同时,才应将它们之间的距离添加到总距离中。

    此外,由于一秒钟内无法移动很远(也许在赛车或火箭中除外),yoi可以增加测量之间的时间间隔。这也有助于节省电池。

    1: https://developer.android.com/reference/android/location/Location