Measuring the Distance Between Two Coordinates in PHP

Measuring the distance between two coordinates in PHP

Not long ago I wrote an example of the haversine formula, and published it on my website:

/**
* Calculates the great-circle distance between two points, with
* the Haversine formula.
* @param float $latitudeFrom Latitude of start point in [deg decimal]
* @param float $longitudeFrom Longitude of start point in [deg decimal]
* @param float $latitudeTo Latitude of target point in [deg decimal]
* @param float $longitudeTo Longitude of target point in [deg decimal]
* @param float $earthRadius Mean earth radius in [m]
* @return float Distance between points in [m] (same as earthRadius)
*/
function haversineGreatCircleDistance(
$latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
{
// convert from degrees to radians
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);

$latDelta = $latTo - $latFrom;
$lonDelta = $lonTo - $lonFrom;

$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
return $angle * $earthRadius;
}

➽ Note that you get the distance back in the same unit as you pass in with the parameter $earthRadius. The default value is 6371000 meters so the result will be in [m] too. To get the result in miles, you could e.g. pass 3959 miles as $earthRadius and the result would be in [mi]. In my opinion it is a good habit to stick with the SI units, if there is no particular reason to do otherwise.

Edit:

As TreyA correctly pointed out, the Haversine formula has weaknesses with antipodal points because of rounding errors (though it is stable for small distances). To get around them, you could use the Vincenty formula instead.

/**
* Calculates the great-circle distance between two points, with
* the Vincenty formula.
* @param float $latitudeFrom Latitude of start point in [deg decimal]
* @param float $longitudeFrom Longitude of start point in [deg decimal]
* @param float $latitudeTo Latitude of target point in [deg decimal]
* @param float $longitudeTo Longitude of target point in [deg decimal]
* @param float $earthRadius Mean earth radius in [m]
* @return float Distance between points in [m] (same as earthRadius)
*/
public static function vincentyGreatCircleDistance(
$latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
{
// convert from degrees to radians
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);

$lonDelta = $lonTo - $lonFrom;
$a = pow(cos($latTo) * sin($lonDelta), 2) +
pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
$b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);

$angle = atan2(sqrt($a), $b);
return $angle * $earthRadius;
}

get distance between two coordinates in km

From http://rosettacode.org/wiki/Haversine_formula#PHP

class POI {
private $latitude;
private $longitude;
public function __construct($latitude, $longitude) {
$this->latitude = deg2rad($latitude);
$this->longitude = deg2rad($longitude);
}
public function getLatitude() return $this->latitude;
public function getLongitude() return $this->longitude;
public function getDistanceInMetersTo(POI $other) {
$radiusOfEarth = 6371000;// Earth's radius in meters.
$diffLatitude = $other->getLatitude() - $this->latitude;
$diffLongitude = $other->getLongitude() - $this->longitude;
$a = sin($diffLatitude / 2) * sin($diffLatitude / 2) +
cos($this->latitude) * cos($other->getLatitude()) *
sin($diffLongitude / 2) * sin($diffLongitude / 2);
$c = 2 * asin(sqrt($a));
$distance = $radiusOfEarth * $c;
return $distance;
}
}

How to calculate distance from lat/long in php?

I did this a few weeks ago.

This link is your best bet:

http://code.google.com/apis/maps/articles/phpsqlsearch.html

Even if you don't use their API, their PHP and SQL query helped really well.

how to get distance between points using latitude and longitude using php?

GeoDataSource team develop a very useful function to achieve this:

function distance($lat1, $lon1, $lat2, $lon2, $unit) {

$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);

if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}

echo distance(32.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles<br>";
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers<br>";
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "N") . " Nautical Miles<br>";

Reference

to calculate driving distance between two coordinates on earth in php

This should get you started...

$json = file_get_contents('http://maps.google.com/maps/nav?q=from:Buderim,Australia%20to:Brisbane,Australia');

$details = json_decode($json, TRUE);

var_dump($details['Directions']['Distance']['meters']);

Calculating distance between two points on a flat plane? (php)

You will need to use a Euclidean distance formula (specifically for 2 dimensions). Basically the formula is:

d = sqrt((x1 - x2)^2 + (y1 - y2)^2)
  • d is your final distance
  • sqrt is the square root (the sqrt() function in PHP)
  • x1 and y1 are your x-y coordinates for your first point
  • x2 and y2 are your x-y coordinates for your second point
  • ^2 means raise to the second power (use the pow() function for PHP)

Pythagoras was the Greek philosopher who developed the Pythagorean Theorem, which can be used to derive the 2-dimensional distance formula (which is different from Euclid's derivation).

Calculate distance between GPS points by coordinates

As pointed out by Roland Starke in the comments, the problem was the order of the coordinates. (7, 47 not 47, 7)

PHP Calc distance between two points result in pixels

You would use Pythagoras Theorem.

$dh = $h1 - $h2;
$dw = $w1 - $w2;
$dist = sqrt($dh*$dh + $dw*$dw);

Note that you may get a non-integer result.

If you're interested in the Manhattan Distance you would just do

$dist = abs($h1 - $h2) + abs($w1 - $w2);


Related Topics



Leave a reply



Submit