【问题标题】:check it current location is near to other locations检查当前位置是否靠近其他位置
【发布时间】:2019-12-12 09:10:27
【问题描述】:

我有 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

          },
        );
  }

【问题讨论】:

标签: flutter dart


【解决方案1】:

我看到你正在使用 Geolocator 插件:

试试这样:

double distanceInMeters = await Geolocator().distanceBetween(lat1, lng1, lat2, lng2);

【讨论】:

    【解决方案2】:

    你可以使用插件调用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
          }); 
    
    },);
    

    【讨论】:

      猜你喜欢
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多