Google map get address from latitude and longitude

In this example, we are going to show you how to convert location coordinates: latitude and longitude to place address using Google Map's geocoding API in Flutter App. We will use Google Map's rest API to fetch addresses. See the example, below:

Before Stating, go to your google console and activate "Geocoding API" on the Google Map platform > API:

Google map get address from latitude and longitude

And also get the API Key from the "credentials" tab.

Google map get address from latitude and longitude

The API key is necessary to authenticate on REST API.

Add geocoding Flutter packages to your project by adding the following lines in pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  geocoding: ^2.0.1

Now get the information about places by longitude and latitude using the code below:

List placemarks = await placemarkFromCoordinates(latitude, longitude);
String palcename = placemarks.first.administrativeArea.toString() + ", " +  placemarks.first.street.toString();

How to Fetch Address of Latitude and Longitude from Google Map API?

In this example, we are going to use the REST API of the Google Map platform to convert the latitude and longitude. You can look at official documentation of Reverse geocoding request and response:

To get Address from Coordinates, you have to request to the following URL format:

https://maps.googleapis.com/maps/api/geocode/json?latlng=27.6669987,85.3071003&key=API_KEY

Replate, the latitude, longitude, and API_KEY with your values. The response from the URL will be JSON data, and it will look like below:

{
   "plus_code" : {
      "compound_code" : "M884+QRX Lalitpur, Nepal",
      "global_code" : "7MV7M884+QRX"
   },
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "M884+PQQ",
               "short_name" : "M884+PQQ",
               "types" : [ "plus_code" ]
            },
            {
               "long_name" : "Ekantakuna",
               "short_name" : "Ekantakuna",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Lalitpur",
               "short_name" : "Lalitpur",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Bagmati",
               "short_name" : "Bagmati",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Bagmati Province",
               "short_name" : "Bagmati Province",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Nepal",
               "short_name" : "NP",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "44600",
               "short_name" : "44600",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "M884+PQQ, Lalitpur 44600, Nepal",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 27.6669181,
                  "lng" : 85.307013
               },
               "southwest" : {
                  "lat" : 27.666812,
                  "lng" : 85.30690349999999
               }
            },
            "location" : {
               "lat" : 27.6668718,
               "lng" : 85.3069672
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 27.66821403029151,
                  "lng" : 85.30830723029149
               },
               "southwest" : {
                  "lat" : 27.6655160697085,
                  "lng" : 85.30560926970848
               }
            }
         },
         "place_id" : "ChIJeQua9S4Y6zkR8qYX17Ia99A",
         "types" : [ "premise" ]
      },

Now, we will use this REST API to convert Location latitude and longitude to address. See the example below for the references:

Before going to the Flutter App example code, first, add dio package in your project by adding the following lines in pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  dio: ^4.0.4

Dio package will be used for HTTP requests.

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

void main(){
  runApp(MyApp());
}

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget{
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State {

  String googleApikey = "API_KEY";
  double latitude = 27.666994; //latitude
  double longitude = 85.309289; //longitude

  String address = "";

  @override
  void initState() {
    convertToAddress(latitude, longitude, googleApikey); //call convert to address 
    super.initState();
  }

  convertToAddress(double lat, double long, String apikey) async {
      Dio dio = Dio();  //initilize dio package
      String apiurl = "https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&key=$apikey";
    
      Response response = await dio.get(apiurl); //send get request to API URL

      if(response.statusCode == 200){ //if connection is successful
          Map data = response.data; //get response data
          if(data["status"] == "OK"){ //if status is "OK" returned from REST API
              if(data["results"].length > 0){ //if there is atleast one address
                 Map firstresult = data["results"][0]; //select the first address

                 address = firstresult["formatted_address"]; //get the address

                 //you can use the JSON data to get address in your own format
                 
                 setState(() {
                    //refresh UI
                 });
              }
          }else{
             print(data["error_message"]);
          }
      }else{
         print("error while fetching geoconding data");
      }  
  }

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
          appBar: AppBar( 
             title: Text("Get Address from Google Map API"),
             backgroundColor: Colors.redAccent,
          ),
          body: Container(
             padding: EdgeInsets.all(30),
             child: Column(
               children: [
                    Text("Latitude: $latitude", style: TextStyle(fontSize: 25),),
                    Text("Longitude: $longitude", style: TextStyle(fontSize: 25)),
                    Text("Formatted Address: $address", 
                                  style: TextStyle(fontSize: 20), 
                                  textAlign: TextAlign.center,),
               ],
             ),
          )
       );
  }
}

You can use this code according to your requirement on your project. The output of this example will look like below:

Google map get address from latitude and longitude

In this way, you can convert location coordinates: latitude and longitude to Place addresses using Google Map Platform REST API in Flutter App.

How do you convert latitude and longitude to address?

Reverse geocoding is the process to convert the latitude and longitude coordinates to a readable address. Type the lat long coordinates and press Convert button. Reverse geocoded address will shown below. Also the municipality, subdivision and country name can be found.

How do I use Google Maps to find an address?

Search for a place on Google Maps.
On your computer, open Google Maps..
Type an address or name of a place..
Press Enter or click Search . To filter your search results, use the drop-down menus below the search box..

How do I punch coordinates into Google Maps?

Enter Coordinates on Android Open Google Maps, enter the coordinates into the Search box at the top, and hit the Search key on the keyboard. You'll see a pin appear on the map for the spot. Swipe up from the bottom to get directions or view details for the location.

How can I get full address from latitude and longitude in Android?

The short story is you need to do: Geocoder geocoder = new Geocoder(this, Locale. getDefault()); List<Address> addresses = geocoder. getFromLocation(lat, lng, 1);