【问题标题】:flutter - Set the max radius when displaying the List distanceflutter - 设置显示列表距离时的最大半径
【发布时间】:2019-07-28 16:54:41
【问题描述】:

我正在开发最近的 Food Dish 应用程序位置。我设法根据离用户最近的位置显示它,使用这个插件great_circle_distance

我想知道如何设置最大半径?前任。

这是我的代码:

 _foodByCategories.sort((a, b) {
      var distance1 = new GreatCircleDistance.fromDegrees(
          latitude1:
              model.userLocation == null ? 0.0 : model.userLocation["latitude"],
          longitude1: model.userLocation == null
              ? 0.0
              : model.userLocation["longitude"],
          latitude2: a.lat,
          longitude2: a.long);
      double totaldistance1 = distance1.haversineDistance();

      var distance2 = new GreatCircleDistance.fromDegrees(
          latitude1:
              model.userLocation == null ? 0.0 : model.userLocation["latitude"],
          longitude1: model.userLocation == null
              ? 0.0
              : model.userLocation["longitude"],
          latitude2: b.lat,
          longitude2: b.long);
      double totaldistance2 = distance2.haversineDistance();
      return (totaldistance1 - totaldistance2).toInt();
    });

任何答案将不胜感激。

【问题讨论】:

    标签: list dart flutter location distance


    【解决方案1】:

    如果我理解你的问题是正确的

    _foodByCategories.where((a) {
      var distance = new GreatCircleDistance.fromDegrees(latitude1: double.parse(widget.lat), longitude1: double.parse(widget.lng), latitude2: double.parse(a.lat), longitude2: double.parse(a.lng));
      var totaldistance = distance.haversineDistance().toStringAsFixed(2);
      double distanceDouble1 = double.parse(totaldistance);
      return distance <= 30000; // or something like that
    }).sort ... // and your sorting code
    

    【讨论】:

    • 你拯救了我的一天兄弟 (y)
    【解决方案2】:

    在您使用的包中,我看不到任何方法可以做到这一点。您可以建议开发人员在其中添加此功能吗?

    或者,您可以在代码中获取total distance,因此您只需执行简单的if statement 即可检查总距离是否大于您设置的最大半径。

    【讨论】:

    • 不幸的是,开发人员不再开发此插件。我已经同意,但 totalDistance1 - totalDistance 2 &lt; 30 的结果但它返回一个布尔值
    • 你的意思是你需要它来返回一个布尔值吗?
    • 我需要返回 Int.. (参见totalDistance1 - totalDistance2 进行排序,然后我将其转换为to.Int()
    • 排序前使用_foodByCategories.where()。将您的距离计算移到外部的单独函数中。 _foodByCategories.where(/* calculate distances and filter in distance_diff &lt; 30*/ ).sort( /* calculate distances and sort by distance_diff */ );
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 2012-12-29
    相关资源
    最近更新 更多