flutter - Set the max radius when displaying the L

2019-07-26 06:51发布

问题:

I am developing the nearest Food Dish application location. I managed to display it based on the location closest to the user, using this plugin great_circle_distance

I want to know how to set the max radius? ex. < 30 Km

Here my code:

 _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();
    });

Any answer will appreciated.

回答1:

If I understand your question correct

_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


回答2:

I can't see any way to do this within the package you are using. You could suggest to the developer to add this functionality in?

Alternatively, you are getting the total distance in your code so you could just do a simple if statement to check if the total distance is greater than your set max radius.