Kitereative Indonesia
Kitereative Indonesia

Reputation: 1217

flutter - Set the max radius when displaying the List distance

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.

Upvotes: 1

Views: 1912

Answers (2)

Andrii Turkovskyi
Andrii Turkovskyi

Reputation: 29458

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

Upvotes: 3

Matt List
Matt List

Reputation: 1973

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.

Upvotes: 0

Related Questions