Issue
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;
}
Solution
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;
}
EDIT
Please use Geocoding instead of Geocoder as Geocoding is maintained by baseflow.com agency.
Answered By - Harsha pulikollu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.