Get distance between two locations with google maps

July 21st, 2020 / by kingjman09

#google-maps #php

You can easily calculate the distance between addresses using Google Maps API and PHP. However, you need to specify the API Key in your request first. 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).

If you’re using wordpress, paste the get getDistance() function in your child theme’s functions.php. Then call the function using the parameters above. Make sure the values also come from google maps to get accurate results.

The source and for more detailed tutorial:
Go to CodexWorld.

If you have suggestions and corrections please feel free to drop it in the comments section.

Leave a Reply

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

eighteen − eight =

close
Kingj Maningo blessed

Thanks for taking the time to reach out. How can I help you?