'Get full address details based on current location's latitude and longitude in Flutter

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 1:[1]

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.

Solution 2:[2]

in pubspec.yaml

geolocator: '^5.1.1'
  geocoder: ^0.2.1

Import this packages

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

Solution 3:[3]

You'll need to use a "reverse geocoding" service, such as Google's API: https://developers.google.com/maps/documentation/geocoding/start. There are others out there as well... just google for "geocoding".

Update: apparently there's a local API as well. See: https://pub.dartlang.org/packages/geocoder

Solution 4:[4]

This is the most simple way to get address from current position or any Latitude and Longitude using Google API.

You have to generate Goole Map API Key from Goole Console [Login Required] Generate API Key From Here

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

Solution 5:[5]

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

Solution 6:[6]

i had done it recently You need GeoCoding Plugin then import this file

import 'package:geocoding/geocoding.dart';

After that, we have to create method.

getUserLocation(){
 List<Placemark> placemarks = await placemarkFromCoordinates(
        currentPostion.latitude, currentPostion.longitude);
    Placemark place = placemarks[0];
   print(place)
}

You can access all property like name,locality using place object like place.name

Solution 7:[7]

Since geocoder plugin as suggested in the above answers is obsolete, please use geolocator and geocoding plugins

import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';


_getLocation() async
{
  Position position = await 
  Geolocator.getCurrentPosition(desiredAccuracy: 
    LocationAccuracy.high);
  debugPrint('location: ${position.latitude}');
  List<Placemark> addresses = await 
  placemarkFromCoordinates(position.latitude,position.longitude);

  var first = addresses.first;
  print("${first.name} : ${first..administrativeArea}");
}

find geocoding pub link geocoding flutter

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Faizan Mubasher
Solution 2 Next Day Software Solution Pvt
Solution 3 Randal Schwartz
Solution 4
Solution 5 Hrafn
Solution 6 Shailandra Rajput
Solution 7 Brian Mutiso