我的Latlng列表包含在lat&lng场所列表中 我想检查一下我当前的位置是否在所有这些地方附近1公里
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
},
);
}
答案 0 :(得分:1)
我看到您正在使用Geolocator插件:
尝试一下:
double distanceInMeters = await Geolocator().distanceBetween(lat1, lng1, lat2, lng2);
答案 1 :(得分:1)
您可以使用插件调用latlong 0.6.1
dependencies:
latlong: ^0.6.1
import 'package:latlong/latlong.dart';
并尝试
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
});
},);