Get Latitude & Longitude From an Address Using PHP and Google Map API

In this tutorial, I am going to show you how to get latitude and longitude of any given address and get the address of any given latitude and longitude using Google Map API.

By using the below Google Map API, we can do it very easily,

http://maps.google.com/maps/api/geocode/json

For example, enter the following URL in your browser with the parameter values address and sensor,

http://maps.google.com/maps/api/geocode/json?address=Chennai+India&sensor=false

It will return the following result as a JSON format,

{
    results:
    [
        {
            address_components: 
            [
                {
                    long_name: "Chennai",
                    short_name: "Chennai",
                    types: [
                        "locality",
                        "political"
                    ]
                },
                {
                    long_name: "Chennai",
                    short_name: "Chennai",
                    types: [
                        "administrative_area_level_2",
                        "political"
                    ]
                },
                {
                    long_name: "Tamil Nadu",
                    short_name: "TN",
                    types: [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    long_name: "India",
                    short_name: "IN",
                    types: [
                        "country",
                        "political"
                    ]
                }
            ],
            formatted_address: "Chennai, Tamil Nadu, India",
            geometry: {
                bounds: {
                    northeast: {
                        lat: 13.2611661,
                        lng: 80.33632279999999
                    },
                    southwest: {
                        lat: 12.8338848,
                        lng: 80.0817007
                    }
                },
                location: {
                    lat: 13.0826802,
                    lng: 80.2707184
                },
                location_type: "APPROXIMATE",
                    viewport: {
                        northeast: {
                            lat: 13.2611661,
                            lng: 80.33632279999999
                        },
                        southwest: {
                            lat: 12.8338848,
                            lng: 80.0817007
                        }
                    }   
                },
                place_id: "ChIJYTN9T-plUjoRM9RjaAunYW4",
                types: [
                    "locality",
                    "political"
                ]
            }
        ],
    status: "OK"
}

PHP Function To Get Data From Google Map API :

In PHP, we can use the function file_get_contents() to get the data from Google Map API URL. While passing the API URL in the file_get_contents(), it will return the result as a JSON object, then use json_decode() function to parse that JSON object result.

Here is the example code to get latitude and longitude of a given address using Google Map API,

<?php
$address = 'Chennai, India';
$url = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=".urlencode($address)."&sensor=false");
$response = json_decode($url);
  
if ($response->status == 'OK') {
    $latitude = $response->results[0]->geometry->location->lat;
    $longitude = $response->results[0]->geometry->location->lng;
    echo 'Latitude: ' . $latitude;
    echo '<br />';
    echo 'Longitude: ' . $longitude;
} else {
    echo $response->status;
}

Here is the example code to get the address of given latitude and longitude using Google Map API,

<?php
$latitude = 13.0826802;
$longitude = 80.2707184;
$url = file_get_contents("http://maps.google.com/maps/api/geocode/json?latlng=$latitude,$longitude&sensor=false");
$response = json_decode($url);
  
if ($response->status == 'OK') {
    echo $address = $response->results[0]->formatted_address;
} else {
    echo $response->status;
}

CURL Function To Get Data From Google Map API :

CURL is another method to get the data from Google Map API, this is the preferred way as it gives much more control, it’s also faster than the PHP function file_get_contents().

Here is the example code to get latitude and longitude of a given address using Google Map API with CURL function,

<?php
$address = "Chennai, India";
$url = "http://maps.google.com/maps/api/geocode/json?address=".urlencode($address);
  
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$responseJson = curl_exec($ch);
curl_close($ch);
  
$response = json_decode($responseJson);
  
if ($response->status == 'OK') {
    $latitude = $response->results[0]->geometry->location->lat;
    $longitude = $response->results[0]->geometry->location->lng;
    echo 'Latitude: ' . $latitude; 
    echo '<br />'; 
    echo 'Longitude: ' . $longitude;
} else {
    echo $response->status;
}

Here is the example code to get the address of given latitude and longitude using Google Map API with CURL function,

<?php
$latitude = 13.0826802;
$longitude = 80.2707184;
  
$url = "http://maps.google.com/maps/api/geocode/json?latlng=$latitude,$longitude&sensor=false";
  
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$responseJson = curl_exec($ch);
curl_close($ch);
  
$response = json_decode($responseJson);
  
if ($response->status == 'OK') {
    echo $address = $response->results[0]->formatted_address;
} else {
    echo $response->status;
}

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *