How to only show post within one mile in Flutter

135

There are multiple way of doing the same depends on requirements:

  1. Get a Current Latitude and Longitude

  2. Create a api that returns destination Postlist along with their PostTimelatitude and a PostTimelatitude in it.

  3. find a difference between them using a below code snippet.

    double distanceInMeters = Geolocator.distanceBetween(
        currentLatitudeTxt,
        currentLongitudeTxt,
        double.parse(posttimeLatitude),
        double.parse(posttimeLatitude),
      );      
    
  4. Convert a distance in Meters to mile and use a listview Widget.

For your reference consider a code snippet below:

  _handleAllRestaurantListResponse(value) {
var arrData = value
    .map<RestaurantModel>((json) => RestaurantModel.fromJson(json))
    .toList();
arrayRestaurantList = arrData;
print("ALL RESTAURANT LENGTH : ${arrayRestaurantList.length}");

if (arrayRestaurantList.isNotEmpty) {
  for (var item in arrayRestaurantList) {
    var responseLatitude = item.latitude;
    var responseLongitude = item.longitude;
    print(responseLatitude);
    print(responseLongitude);
    if (responseLatitude != null && responseLongitude != null) {
      double distanceInMeters = Geolocator.distanceBetween(
        currentLatitudeTxt,
        currentLongitudeTxt,
        double.parse(responseLatitude),
        double.parse(responseLongitude),
      );
      double distanceInKm = distanceInMeters / 1000;
      int roundedDistanceInKM = distanceInKm.toInt();
      if (nearByRestaurantRadius > roundedDistanceInKM) {
        filterRestaurantList.add(item);
      }
    }
  }
} else {
  print("object");
}

}

Share:
135
Saesun Kim
Author by

Saesun Kim

Updated on December 30, 2022

Comments

  • Saesun Kim
    Saesun Kim over 1 year

    I am trying to make a functionality that can show nearby posts from the user.

    From my code, first I am reading data from the firebase (timelineLocalRef), such that

    getTimelineLocal() async {
    QuerySnapshot snapshotLocal = await timelineLocalRef
        .doc('test')
        .collection('userPosts')
        .orderBy('timestamp', descending: true)
        .get();
    
    List<PostL> postsLocal =
        snapshotLocal.docs.map((doc) => PostL.fromDocument(doc)).toList();
    setState(() {
      this.postsLocal = postsLocal;
    });
    

    In timelineRef, information for posts are saved such that,

    enter image description here

    Once I have obtained the post information, I am also taking the user location using the Geolocator.

      getUserLocation() async {
        Position position = await Geolocator()
            .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
        posXuser = position.latitude;
        posYuser = position.longitude;
      }
    

    With the user location (posXuser, posYuser) and the posting location (posX,posY), I should be able to calculate the distance between two using the function below.

      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 0.621371 * 12742 * asin(sqrt(a));
        //return mile distance
      }
    

    So What I want is from the first code, when I make postsLocal, I want to only import the post within 1 mile away. Can you help me with how to make a conditional mapping snapshots?