【问题标题】:How to get latitude and longitude in flutter如何在颤动中获得经纬度
【发布时间】:2020-03-25 03:32:15
【问题描述】:

我可以弄清楚如何将用户的纬度和经度放在一起,但需要单独获取它们以利用 Geolocator 包中的 Geolocator().distanceBetween 函数。我下面的代码不断收到错误:

未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'latitude'。

接收者:空

尝试调用:纬度

  Future<Position> getLocation() async {
    var currentLocation;
    try {
      currentLocation = await geolocator.getCurrentPosition(
          desiredAccuracy: LocationAccuracy.best);
    } catch (e) {
      currentLocation = null;
    }
    return currentLocation;
  }


  void calculateDistance() async {

    getLocation().then((position) {
       userLocation = position;
    });

      final double myPositionLat =  userLocation.latitude;
      final double myPositionLong = userLocation.longitude;

      final double TPLat = 51.5148731;
      final double TPLong = -0.1923663;
      final distanceInMetres = await Geolocator().distanceBetween(myPositionLat, myPositionLong, TPLat, TPLong)/1000;

      print(distanceInMetres);

  }

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    试试这个:

    void calculateDistance() async {
    
    getLocation().then((position) {
       userLocation = position;
    
    
      final double myPositionLat =  userLocation.latitude;
      final double myPositionLong = userLocation.longitude;
    
      final double TPLat = 51.5148731;
      final double TPLong = -0.1923663;
      final distanceInMetres = await Geolocator().distanceBetween(myPositionLat, myPositionLong, TPLat, TPLong)/1000;
    
      print(distanceInMetres);
    
      });
    }
    

    或另一种方法是:

    void calculateDistance() async {
    
    userLocation = await getLocation();
    
    
      final double myPositionLat =  userLocation.latitude;
      final double myPositionLong = userLocation.longitude;
    
      final double TPLat = 51.5148731;
      final double TPLong = -0.1923663;
      final distanceInMetres = await Geolocator().distanceBetween(myPositionLat, myPositionLong, TPLat, TPLong)/1000;
    
      print(distanceInMetres);
    }
    

    【讨论】:

    • Broooo 你救了我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 2019-10-11
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    相关资源
    最近更新 更多