我在 flutter 中使用了 location插件,我只能获得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;
}
答案 0 :(得分:1)
您需要使用“反向地理编码”服务,例如Google的API:https://developers.google.com/maps/documentation/geocoding/start。还有其他...只是谷歌的“地理编码”。
更新:显然也有本地API。参见:https://pub.dartlang.org/packages/geocoder
答案 1 :(得分:1)
在pubspec.yaml
中geolocator: '^5.1.1'
geocoder: ^0.2.1
导入此软件包
import 'package:geolocator/geolocator.dart';
import 'package:geocoder/geocoder.dart';
_getLocation() async
{
Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
debugPrint('location: ${position.latitude}');
final coordinates = new Coordinates(position.latitude, position.longitude);
var addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);
var first = addresses.first;
print("${first.featureName} : ${first.addressLine}");
}
答案 2 :(得分:0)
使用Geocoder插件,您可以从经度和经度获得地址
getUserLocation() async {//call this async method from whereever you need
Map<String, double> currentLocation = new Map();
Map<String, double> 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(
currentLocation['latitude'], currentLocation['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;
}
答案 3 :(得分:0)
`
import 'package:flutter/material.dart';
import 'package:geocoder/geocoder.dart';
import 'package:geolocator/geolocator.dart';
_getLocation() async {
GeolocationStatus geolocationStatus = await
Geolocator().checkGeolocationPermissionStatus();
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
debugPrint('location: ${position.latitude}');
final coordinates = new Coordinates(position.latitude, position.longitude);
debugPrint('coordinates is: $coordinates');
var addresses =
await Geocoder.local.findAddressesFromCoordinates(coordinates);
var first = addresses.first;
// print number of retured addresses
debugPrint('${addresses.length}');
// print the best address
debugPrint("${first.featureName} : ${first.addressLine}");
//print other address names
debugPrint(Country:${first.countryName} AdminArea:${first.adminArea} SubAdminArea:${first.subAdminArea}");
//print more address names
debugPrint(Locality:${first.locality}: Sublocality:${first.subLocality}");
}
`
答案 4 :(得分:0)
这是使用 Google API 从当前位置或任何纬度和经度获取地址的最简单方法。
getAddressFromLatLng(context, double lat, double lng) async {
String _host = 'https://maps.google.com/maps/api/geocode/json';
final url = '$_host?key=$mapApiKey&language=en&latlng=$lat,$lng';
if(lat != null && lng != null){
var response = await http.get(Uri.parse(url));
if(response.statusCode == 200) {
Map data = jsonDecode(response.body);
String _formattedAddress = data["results"][0]["formatted_address"];
print("response ==== $_formattedAddress");
return _formattedAddress;
} else return null;
} else return null;
}