我在flutter中使用了location plugin,只能得到latitude
和longitude
。如何获取完整的地址详细信息。代码如下。
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/