How To Distance Between Two Addresses Google Maps Api PHP

How To Distance Between Two Addresses Google Maps Api PHP

How To Distance Between Two Addresses Google Maps Api PHP

0 Sales


To translate the address into geographic coordinates, the geocoding process is used. The Google Maps Geocoding API provides an easy way to collect geographic address information. In general, the Geocoding API is used in the web application to obtain the latitude and longitude of an address. In addition, the Google Maps Geocoding API can be used for many Geocoding data-related purposes.


When your web application works with the location of the user, the distance calculation is useful. With the Google Maps API and PHP, you can easily calculate the distance between addresses. In this guide, with Google Maps Geocoding API using PHP, we'll show you how to calculate the distance between two addresses.


You should define the API key in your application to use the Google Maps Geocoding API. Build an API key on the Google Cloud Platform Console for the Geocoding API before beginning.


All the distance calculation code is grouped into a PHP function to make it reusable. The getDistance() function accepts three parameters and calculates the distance between two address using Google Maps Geocoding API and PHP.


  • $addressFrom – Required. Staring point address.

  • $addressTo – Required. The endpoint address.

  • $unit – Optional. The default unit is miles, You can specify your desired unit (K – kilometer, M – meters).


Calculate Distance Between Address 


Using PHP, getDistance() helps to get the distance between two addresses.


  • Specify your API key for Google Maps.

  • Change the address format.

  • Initiate the request for the Geocoding API and specify the format and address for the geographic data to be collected.

  • Retrieve the geodata latitude and longitude.

  • Calculate the latitude-longitude range.

  • Convert the module and the distance to return.



 * @function getDistance()

 * Calculates the distance between two address


 * @params

 * $addressFrom - Starting point

 * $addressTo - End point

 * $unit - Unit type


 * @author CodexWorld

 * @url



function getDistance($addressFrom, $addressTo, $unit = ''){

    // Google API key

    $apiKey = 'Your_Google_API_Key';


    // Change address format

    $formattedAddrFrom    = str_replace(' ', '+', $addressFrom);

    $formattedAddrTo     = str_replace(' ', '+', $addressTo);


    // Geocoding API request with start address

    $geocodeFrom = file_get_contents(''.$formattedAddrFrom.'&sensor=false&key='.$apiKey);

    $outputFrom = json_decode($geocodeFrom);


        return $outputFrom->error_message;



    // Geocoding API request with end address

    $geocodeTo = file_get_contents(''.$formattedAddrTo.'&sensor=false&key='.$apiKey);

    $outputTo = json_decode($geocodeTo);


        return $outputTo->error_message;



    // Get latitude and longitude from the geodata

    $latitudeFrom    = $outputFrom->results[0]->geometry->location->lat;

    $longitudeFrom    = $outputFrom->results[0]->geometry->location->lng;

    $latitudeTo        = $outputTo->results[0]->geometry->location->lat;

    $longitudeTo    = $outputTo->results[0]->geometry->location->lng;


    // Calculate distance between latitude and longitude

    $theta    = $longitudeFrom - $longitudeTo;

    $dist    = sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) +  cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) * cos(deg2rad($theta));

    $dist    = acos($dist);

    $dist    = rad2deg($dist);

    $miles    = $dist * 60 * 1.1515;


    // Convert unit and return distance

    $unit = strtoupper($unit);

    if($unit == "K"){

        return round($miles * 1.609344, 2).' km';

    }elseif($unit == "M"){

        return round($miles * 1609.344, 2).' meters';


        return round($miles, 2).' miles';




Call the getDistance() function and transfer two addresses to measure distance respectively.


$addressFrom = 'Insert start address';

$addressTo   = 'Insert end address';

// Get distance in km

$distance = getDistance($addressFrom, $addressTo, "K");


You can use it for personal or commercial projects. You can't resell it partially or in this form.


Create Date : May 14, 2020

Updated Date : May 14, 2020


Comments : 0

Downloads : 0