根据Flutter中当前位置的经度和纬度获取完整的地址详细信息 [英] Get full address details based on current location's latitude and longitude in Flutter

查看:643
本文介绍了根据Flutter中当前位置的经度和纬度获取完整的地址详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 flutter 中使用了 location插件,我只能获得纬度经度.如何获取完整的地址详细信息.代码在下面.

I have used the location plugin in the flutter, I can get the latitude and longitude only. How to get the full address details. Codes on below.

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 插件您可以从经纬度获取地址

Using Geocoder plugin you can get address from the latitiude and longitude

 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;
    }

编辑

请使用地理编码,而不要使用 baseflow.com 代理商.

Please use Geocoding instead of Geocoder as Geocoding is maintained by baseflow.com agency.

这篇关于根据Flutter中当前位置的经度和纬度获取完整的地址详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆