1

I have list of Latlng that contains on list of places lat&lng and I want check my current location is near of any of these places by 1 kilo

static locationListen() async {

    List<LatLng> savedPlace = [
      LatLng(12.333,-344.2222),
      LatLng(1.333,-14.2222),
      LatLng(2.333,-24.2222),
    ]; 

    var locationOptions =
        LocationOptions(accuracy: LocationAccuracy.high, distanceFilter: 10);

    Geolocator().getPositionStream(locationOptions).listen(
          (Position position) {
            LatLng currentLatlng = LatLng(position.latitude,position.longitude);

            //check this if  is this currentLatlng near by 1 kilo from
            // any of those places savedPlace

          },
        );
  }
Mahmoud Niypoo
  • 1,598
  • 5
  • 24
  • 41

2 Answers2

1

I see you're using Geolocator plugin:

Take a try like that:

double distanceInMeters = await Geolocator().distanceBetween(lat1, lng1, lat2, lng2);
duongdt3
  • 1,648
  • 11
  • 16
1

you can use plugin call latlong 0.6.1

dependencies:
  latlong: ^0.6.1

import 'package:latlong/latlong.dart';

and try it

Geolocator().getPositionStream(locationOptions).listen( (Position position) {

 LatLng currentLatlng = LatLng(position.latitude,position.longitude); 

   savedPlace.forEach((savedlatlong) { 
       int km = distance.as(LengthUnit.Kilometer,currentLatlng,savedlatlong);
       // do whatever you wanna do with km
      }); 

},);
Jaydeep chatrola
  • 2,423
  • 11
  • 17