By directing location directly to Google Map it will always be a good idea to transfer and length with the Google Maps API. In this article, you will learn how to find the latitude and longitude of an address using the Google Maps API in PHP. Here we will integrate all the code into PHP functionality and you only need to use this function to get latitude and length from address.

getLatLong() function receives one parameter ($address), at the $ address you need to provide the full address from where you want to get the length and length. This function returns latitude and address length as the same members if found, otherwise, return FALSE.


            function getLatLong($address){
        //Formatted address
        $formattedAddr = str_replace(' ','+',$address);
        //Send request and receive json data by address
        $geocodeFromAddr = file_get_contents(''.$formattedAddr.'&sensor=false'); 
        $output = json_decode($geocodeFromAddr);
        //Get latitude and longitute from json data
        $data['latitude']  = $output->results[0]->geometry->location->lat; 
        $data['longitude'] = $output->results[0]->geometry->location->lng;
        //Return latitude and longitude of the given address
            return $data;
            return false;
        return false;   

Use getLatLong() function such as the following.

$address = 'White House, Pennsylvania Avenue Northwest, Washington, DC, United States';
$latLong = getLatLong($address);
$latitude = $latLong['latitude']?$latLong['latitude']:'Not found';
$longitude = $latLong['longitude']?$latLong['longitude']:'Not found';

To specify a Google API key in your request, include it as the value of a key parameter.