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.