android - 在Flutter中根据当前位置的经纬度获取完整的地址详细信息

标签 android flutter

我在flutter中使用了location plugin,只能得到latitudelongitude。如何获取完整的地址详细信息。代码如下。

Future<Map<String, double>> _getLocation() async {
//var currentLocation = <String, double>{};
Map<String,double> currentLocation;
try {
  currentLocation = await location.getLocation();
} catch (e) {
  currentLocation = null;
}
setState(() {
  userLocation = currentLocation;
});
return currentLocation;



}

最佳答案

使用 Geocoder插件你可以从纬度和经度获取地址

 import 'package:location/location.dart';
 import 'package:geocoder/geocoder.dart';
 import 'package:flutter/services.dart';

getUserLocation() async {//call this async method from whereever you need
    
      LocationData myLocation;
      String error;
      Location location = new Location();
      try {
        myLocation = await location.getLocation();
      } on PlatformException catch (e) {
        if (e.code == 'PERMISSION_DENIED') {
          error = 'please grant permission';
          print(error);
        }
        if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {
          error = 'permission denied- please enable it from app settings';
          print(error);
        }
        myLocation = null;
      }
      currentLocation = myLocation;
      final coordinates = new Coordinates(
          myLocation.latitude, myLocation.longitude);
      var addresses = await Geocoder.local.findAddressesFromCoordinates(
          coordinates);
      var first = addresses.first;
      print(' ${first.locality}, ${first.adminArea},${first.subLocality}, ${first.subAdminArea},${first.addressLine}, ${first.featureName},${first.thoroughfare}, ${first.subThoroughfare}');
      return first;
    }

编辑

请使用Geocoding而不是 Geocoder因为 地理编码baseflow.com 维护机构。

关于android - 在Flutter中根据当前位置的经纬度获取完整的地址详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54761683/

相关文章:

ios - 错误 : 'Flutter/Flutter.h' file not found when flutter run on iOS

android - zxing 二维码扫描仪摄像头显示白屏

Android:无法在模拟器中运行 xxhdpi-drawable

flutter - 如何从 Flutter 桌面应用程序中打开 CMD

Flutter Dart 功能弹出错误的小部件?

flutter - 有没有更好的方法来将数据传递给没有状态的,继承的小部件的子级?

java - Android Studio Gradle 项目 "Unable to start the daemon process/initialization of VM"

java - Android 如何沿着手指画一条平滑线

java - libGDX - 纹理存储在哪里?

dart - Flutter - BLoC 模式 - 如何使用流调用另一个小部件的方法,即动画?