определение расстояний между двумя точками по координатам GPS

How can I determine the distance between two points on a map using GPS coordinates?

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:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GPS to kilometers</title>
<meta name="author" content="Oleksiy Lavrynenko | https://lavrynenko.com" />
<meta name="description" content="GPS to kilometers" />
<meta name="robots" content="index,follow" />
<!-- Connecting stylesheet -->
<link rel="stylesheet" href="d-style.css">
<!-- Tab icon -->
<link rel="icon" href="html-to-telegram.png">
</head>
<body>
<!-- Form for entering coordinates -->
<form class="telegram_id_form" action="#" method="post">
<?php
// Tooltip texts for input fields
$coords1 = "Enter coordinates of point 1";
$coords2 = "Enter coordinates of point 2."
// Text on the button
$button = "Calculate distance";
?>
<!-- Fields for entering coordinates -->
<input class="coords" name="coords1" id="coords1" type="text" placeholder="<?php echo "$coords1";?>">">
<input class="coords" name="coords2" id="coords2" type="text" placeholder="<?php echo "$coords2";?>">" >
<!-- Form submit button -->
<input class="button" name="submit" type="submit" value="<?php echo "$button";?>" />
<!-- Textarea field for outputting the result -->
<textarea class="textarea" name="textarea" id="textarea" rows="5"><?php if(isset($distance)) echo "Distance between points: " . number_format($distance, 5) . " km"; ?"></textarea>
</form>
<?php
// Check if the form was submitted
if(isset($_POST['submit'])) {
// Earth's radius in kilometers
$r = 6371;
// Getting the entered coordinates
$coords1 = $_POST['coords1'];
$coords2 = $_POST['coords2'];
// Split the coordinates into latitude and longitude
$coords1_parts = explode(',', $coords1);
$coords2_parts = explode(',', $coords2);
// Check that the arrays contain both elements
if(count($coords1_parts) == 2 &#038;&#038; count($coords2_parts) == 2) {
// Extract coordinates
$lat1 = (float)trim($coords1_parts[0]);
$lon1 = (float)trim($coords1_parts[1]);
$lat2 = (float)trim($coords2_parts[0]);
$lon2 = (float)trim($coords2_parts[1]);
// Conversion of coordinates from degrees to radians
$lat1 = deg2rad($lat1);
$lon1 = deg2rad($lon1);
$lat2 = deg2rad($lat2);
$lon2 = deg2rad($lon2);
// Calculate the difference in coordinates
$dLon = abs($lon2 - $lon1);
$dLat = abs($lat2 - $lat1);
// Calculate sines and cosines of half differences of latitudes and longitudes
$a = sin($dLat / 2) * sin($dLat / 2) + cos($lat1) * cos($lat2) * sin($dLon / 2) * sin($dLon / 2);
// Calculate angular distance
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
// Calculation of the actual distance
$distance = $r * $c;
// Passing the result to the form
echo "<script>document.getElementById('textarea').value = 'Distance between points: " . number_format($distance, 5) . " km';</script>"
} else {
// Output a coordinate format error message
echo "Incorrect coordinate format. Enter coordinates in 'latitude, longitude' format for both points.".
}
}
?>
</body>
</html>

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! ❤️

PayPal Logo Donate via PayPal

Revolut Logo Donate via Revolut