Hey everyone,
I'm new to PHP and I'm currently working on a project where I need to calculate the distance between two locations using the Google Maps API. I was wondering if any of you could help me with a code snippet to achieve this?
I want the program to take two inputs, the latitude and longitude of two locations, and then use the Google Maps API to calculate the distance between them. Finally, I would like the program to display the distance on a map, if possible.
Any help or code snippets would be greatly appreciated. Thanks in advance!

Hello there,
I happened to come across this thread and wanted to share my personal experience with calculating distances between locations using the Google Maps API. I encountered a similar requirement recently in a project and would like to provide an alternative solution that worked well for me.
Instead of utilizing the Geocoding API and Haversine formula, I found it more convenient to employ the Google Maps Distance Matrix API. This API allows you to obtain distance-related information between two or more locations directly, including distance, duration, and even real-time traffic data.
Allow me to share a code snippet to demonstrate how this can be achieved:
In this code, you'll need to replace `'Origin location'` and `'Destination location'` with the actual address or coordinates of your origin and destination. Also, don't forget to replace `'YOUR_API_KEY'` with your valid Google Maps API key.
By making a request to the Distance Matrix API, you can directly obtain the distance between the two locations without performing any extra calculations or conversions. The API response includes various details related to distance, duration, and even additional information like traffic data if you need it.
Feel free to give this approach a shot and let me know if you encounter any issues or have further questions. Happy coding!