Find The Distance Between two Addresses using Google API and PHP

Last updated on by , 9 comments


In this article, you are going to learn how you can find the distance between two addresses. We are going to use google map API and PHP to accomplish this task.

View Demo

The distance calculator module we are going to develop here is basically an API provided by Google itself. This API can measure the driving distance between two addresses as well as Travel Time. You can also measure distance between cities using this module.

The format for origin location and destination location could be one of the following:

  • place name (e.g. Bhopal, India)
  • ZIP code (e.g. 462016)
  • latitude/longitude coordinates (e.g. 23.77xxx, 77.30xxx)

to know more about permissible formats please visit Google’s official documentation page.

Find Distance Between two Addresses

Step 1:- Get a google’s API Key from https://developers.google.com/maps/documentation/distance-matrix/get-api-key.

click on GET A KEY button

find distance between two addresses

now click on +create a new project and enter your project name then click on ENABLE API button

distance calculator api

then after you are finally prompted to a popup window containing your API KEY.

calculate distance between two cities

Step 2: source code


<!DOCTYPE html>
<html>
    <body>

        <form action="" method="post">

            <label>Origin:</label> <input type="text" name="o" placeholder="Enter Origin location" required> <br><br>
            <label>Destination:</label> <input type="text" name="d" placeholder="Enter Destination location" required> <br><br>
            <input type="submit" value="Calculate distance & time" name="submit"> <br><br>

        </form>

        <?php
            if(isset($_POST['submit'])){
            $origin = $_POST['o']; $destination = $_POST['d'];
            $api = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=".$origin."&destinations=".$destination."&key=YOUR_API_KEY");
            $data = json_decode($api);
        ?>

            <label><b>Distance: </b></label> <span><?php echo ((int)$data->rows[0]->elements[0]->distance->value / 1000).' Km'; ?></span> <br><br>
            <label><b>Travel Time: </b></label> <span><?php echo $data->rows[0]->elements[0]->duration->text; ?></span> 

        <?php } ?>

    </body>
</html>

replace YOUR_API_KEY with your API KEY (Step 1) in above code.

We have covered find distance between two addresses module in this article, to get latest updates please subscribe our blog and follow us on Facebook. Don’t forget to share this article with your friends.

You may also like



9 responses to “Find The Distance Between two Addresses using Google API and PHP”

  1. harris says:

    Well great information paritosh. explained in detail. Thanks for sharing.

  2. Christos says:

    Very good explaining and clean code, well done. I wish you could consider some demo preview…

    Keep us posted…

  3. Hey just wanted to give you a quick heads up. The words in your article seem
    to be running off the screen in Safari. I’m not sure if this is
    a format issue or something to do with browser compatibility but I thought I’d post to let you
    know. The design look great though! Hope you get the problem solved soon. Many thanks

  4. Alex says:

    Nice, but is there a way to get suggestion when you type the address? For instance if you type Paris to see: Paris, France, Paris Museum….?

Leave a Reply

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