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 how to 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,

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

It will return the following JSON result,

{
    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 :

We can use the PHP function file_get_contents() to get the data from Google Map API. Pass the API URL to file_get_contents() and this will return the JSON object as result, then use json_decode() function to parse that JSON object.

Here is the example code to get latitude and longitude of 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 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 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 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 you.

Leave a Reply

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