I have a strong need to make something using PHP, with the help of which it will be possible to quickly determine the distance between two points on the map by GPS coordinates. For those who are a bit out of the loop – let me clarify that the main difficulty of such a task is that we need to determine the distance between two points on a sphere, not on a plane. Therefore, we need (there are analogs, but I prefer to use) the Haversine method.
Haversine’s method is based on trigonometric functions and laws of spherical geometry and allows you to calculate the distance between two points on the surface of a sphere using their geographic coordinates (latitude and longitude). This method takes into account the curvature of the Earth and gives more accurate results, especially for long distances, than a simple straight-line distance on a plane.
I’ve already described a scheme for solving a similar question using Python (the solution is here) – but I’ll say it again – I needed to do a similar one, but now in PHP. It took about 30 minutes, with a break for tea and music (I used this as music):
… In visual design everything is simple – a form with two fields where you enter the coordinates of the first and second point, and – respectively, a button, when you click on which all the magic happens 🙂 It was lazy to output all the magic in the second file, so everything is done in one file, the code of which is below. Use it if you need it:
/* Error on d-style.css : Something went wrong: A valid URL was not provided. */
document.getElementById('textarea').value = 'Distance between points: " . number_format($distance, 5) . " km';"
} else {
// Output a coordinate format error message
echo "Incorrect coordinate format. Enter coordinates in 'latitude, longitude' format for both points.".
}
}
?>
There’s a bunch of comments in the code – it’ll sort of make sense.
In working form, the mechanism for determining the distance between two points on the map by GPS coordinates can be either link, or – here, the box in the frame 🙂 :
As always – email, or Telegram if you have any questions.
Support the Blog!
Running a blog takes a lot of effort, time, and passion. Your donations help improve the content, inspire new ideas, and keep the project going.
If you’ve enjoyed the blog’s materials, any support would mean the world to me. Thank you for being here! ❤️