Measuring the distance between two coordinates in PHP
Asked Answered
S

13

188

Hi I have the need to calculate the distance between two points having the lat and long.

I would like to avoid any call to external API.

I tried to implement the Haversine Formula in PHP:

Here is the code:

class CoordDistance
 {
    public $lat_a = 0;
    public $lon_a = 0;
    public $lat_b = 0;
    public $lon_b = 0;

    public $measure_unit = 'kilometers';

    public $measure_state = false;

    public $measure = 0;

    public $error = '';



    public function DistAB()

      {
          $delta_lat = $this->lat_b - $this->lat_a ;
          $delta_lon = $this->lon_b - $this->lon_a ;

          $earth_radius = 6372.795477598;

          $alpha    = $delta_lat/2;
          $beta     = $delta_lon/2;
          $a        = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($this->lat_a)) * cos(deg2rad($this->lat_b)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
          $c        = asin(min(1, sqrt($a)));
          $distance = 2*$earth_radius * $c;
          $distance = round($distance, 4);

          $this->measure = $distance;

      }
    }

Testing it with some given points which have public distances I don't get a reliable result.

I don't understand if there is an error in the original formula or in my implementation

Simian answered 7/4, 2012 at 9:35 Comment(1)
I found working code here in many languages including php geodatasource.com/developers/phpTorture
G
363

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;
}
Ga answered 7/4, 2012 at 12:0 Comment(17)
@TreyA - There are different versions possible, this version implements the formula in Wikipedia and is well tested. The $angle means the angle in the middle of the world in radians, so one can multiplicate it with the earth radius. I can also provide an example of the more complex Vincenty formula if someone is interested.Ga
@TreyA - Yes i know, i'm not sure what you want to say with that. Have you tested the function and did it calculate a wrong result? And have you looked at the formula in Wikipedia? You should really do a test of your own and give me an example of what you think is calculated wrong.Ga
Sorry, but i have to explain some things now. 1) The question was about the Haversine formula, you should tell us if you suggest to use another formula. 2) The Haversine formula has weaknesses around the poles, but is accurate for small distances (it's a problem of the arccosine formula). 3) You stated that there is a step missing with the calculated $angle, that's simply wrong, it cannot improve the result, please please test it out! 4) I agree that it would be better to use the stable Vincenty formula, i already offered to give an example. Maybe you could write an answer as well?Ga
@martinstoekli - you are correct, you do not have a step missing in your Haversine formula. I removed my comments as to not confuse future readers.Crabstick
@capikaw - As you can see in the comments, the units are decimal degrees and meters. Actually the unit you pass to the parameter $earthRadius determines the unit of the result, that means, if you pass km as earth radius, you will get the distance in km.Ga
how big is a small distance? pleaseBlandish
@Blandish - Acually it doesn't matter, both formulas, Haversine and Vincenty are stable for small distances. It is another formula, the arccosine formula having problems, search for "rounding errors" in the linked Wikipedia article.Ga
@Ga thank you, but I am trying to figure out if the distances I plan on calculating are small or big... It is very subjective...Blandish
@Blandish - Yes this is of course relative, one meter can be both long or short, you would have to say more about your intentions. If you are concerned about the rounding errors, why using the arccosine function? You can easily calculate yourself what a rounding error of $angle means for the distance, just take the product of the error with the earth radius: $roundingError * $earthRadius.Ga
And how to convert the value in Miles ? In what unit is the returned value??? Please clarify !Guv
@Pratik - It is the same unit as you put in the earth radius. In the example this is in meter. See the comment above to capikaw.Ga
@Ga , see , people wont be reading comments but the answer only. You really need to edit the answer and tell what parameter to pass to get distance in miles. Lots of users like me come here for answer regarding Distance in miles and get disappointed. So please consider editing the ansewer. I dont think it will take more than 45 minutes to edit the answer. ThanksGuv
@PratikCJoshi - Finally found the time to add a note about using different units.Ga
To anyone looking for the result in miles. Multiply the meters result by 0.000621371192 and you will have your miles.Tempo
does anyone have the php code for finding the coordinates after traveling in a set direction for x miles, i found i there but it is in JS. movable-type.co.uk/scripts/latlong.htmlPostorbital
I've tried vincentyGreatCircleDistance and the result is already in KM not in metersJustice
I just want to point out this function have a great documentation. Thanks for sharing.Interesting
S
81

I found this code which is giving me reliable results.

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;
  }
}

results :

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>";
Stopoff answered 31/5, 2015 at 11:28 Comment(4)
great stuff, I tried this and also google maps shows same distance only decimal changes here and there..Toddle
What if you want to calculate the distance between three points?Mota
call this function two times and add them up, alternately you change the function accordinglyStopoff
returns NaN in some conditions #37184759Blondie
A
31

It's just addition to @martinstoeckli and @Janith Chinthana answers. For those who curious about which algorithm is fastest i wrote the performance test. Best performance result shows optimized function from codexworld.com:

/**
 * Optimized algorithm from http://www.codexworld.com
 *
 * @param float $latitudeFrom
 * @param float $longitudeFrom
 * @param float $latitudeTo
 * @param float $longitudeTo
 *
 * @return float [km]
 */
function codexworldGetDistanceOpt($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo)
{
    $rad = M_PI / 180;
    //Calculate distance from latitude and longitude
    $theta = $longitudeFrom - $longitudeTo;
    $dist = sin($latitudeFrom * $rad) 
        * sin($latitudeTo * $rad) +  cos($latitudeFrom * $rad)
        * cos($latitudeTo * $rad) * cos($theta * $rad);

    return acos($dist) / $rad * 60 *  1.853;
}

Here is test results:

Test name       Repeats         Result          Performance     
codexworld-opt  10000           0.084952 sec    +0.00%
codexworld      10000           0.104127 sec    -22.57%
custom          10000           0.107419 sec    -26.45%
custom2         10000           0.111576 sec    -31.34%
custom1         10000           0.136691 sec    -60.90%
vincenty        10000           0.165881 sec    -95.26%
Aluminothermy answered 2/12, 2016 at 9:55 Comment(7)
In your code the multiplier for the codexworlds algorithms is 1.852, whereas the actual original is 1.1515. Why is this? Why the difference?Dallis
@Dallis Original mulitplier is for miles. Optimized function returns result in km. 1.1515 * 1.609344 = 1.853. Thanks, fixed to 1.853.Aluminothermy
Why don't you use M_PI / 180 and $rad * 60 * 1.853 as constants for better performance?Fer
@EvrenYurtesen Nice idea if your priority is performance. But maintability and readability will become more complicated i think.Aluminothermy
Just put a comment on previous line and say // M_PI / 180 ... etc. I don't know why it would make it difficult to maintain. It is not something you will ever change.Fer
@EvrenYurtesen Tryed to follow your advice and got precision issue. var_dump(M_PI / 180) returns float(0.017453292519943). var_dump((M_PI / 180 * 60 * 1.853) === (0.017453292519943 * 60 * 1.853)) returns false. codepad.org/6DNtuOOcAluminothermy
@Alexander Yancharuk that is expected when working with floating point numbers. This is not an error. See: php.net/manual/en/language.types.float.php You probably do not need so high precision until the last digit anyway. Your code only shows there is small precision difference. It doesn't mean which result is more precise. It is up to your requirements.Fer
C
16

Here the simple and perfect code for calculating the distance between two latitude and longitude. The following code have been found from here - http://www.codexworld.com/distance-between-two-addresses-google-maps-api-php/

$latitudeFrom = '22.574864';
$longitudeFrom = '88.437915';

$latitudeTo = '22.568662';
$longitudeTo = '88.431918';

//Calculate distance from latitude and longitude
$theta = $longitudeFrom - $longitudeTo;
$dist = sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) +  cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;

$distance = ($miles * 1.609344).' km';
Cove answered 19/4, 2016 at 8:45 Comment(0)
N
16

Quite old question, but for those interested in a PHP code that returns the same results as Google Maps, the following does the job:

/**
 * Computes the distance between two coordinates.
 *
 * Implementation based on reverse engineering of
 * <code>google.maps.geometry.spherical.computeDistanceBetween()</code>.
 *
 * @param float $lat1 Latitude from the first point.
 * @param float $lng1 Longitude from the first point.
 * @param float $lat2 Latitude from the second point.
 * @param float $lng2 Longitude from the second point.
 * @param float $radius (optional) Radius in meters.
 *
 * @return float Distance in meters.
 */
function computeDistance($lat1, $lng1, $lat2, $lng2, $radius = 6378137)
{
    static $x = M_PI / 180;
    $lat1 *= $x; $lng1 *= $x;
    $lat2 *= $x; $lng2 *= $x;
    $distance = 2 * asin(sqrt(pow(sin(($lat1 - $lat2) / 2), 2) + cos($lat1) * cos($lat2) * pow(sin(($lng1 - $lng2) / 2), 2)));

    return $distance * $radius;
}

I've tested with various coordinates and it works perfectly.

I think it should be faster then some alternatives too. But didn't test that.

Hint: Google Maps uses 6378137 as Earth radius. So using it with other algorithms might work as well.

Nonsuch answered 30/10, 2018 at 16:47 Comment(0)
F
8

For the ones who like shorter and faster(not calling deg2rad()).

function circle_distance($lat1, $lon1, $lat2, $lon2) {
  $rad = M_PI / 180;
  return acos(sin($lat2*$rad) * sin($lat1*$rad) + cos($lat2*$rad) * cos($lat1*$rad) * cos($lon2*$rad - $lon1*$rad)) * 6371;// Kilometers
}
Filicide answered 12/7, 2015 at 11:54 Comment(0)
V
4

Try this function out to calculate distance between to points of latitude and longitude

function calculateDistanceBetweenTwoPoints($latitudeOne='', $longitudeOne='', $latitudeTwo='', $longitudeTwo='',$distanceUnit ='',$round=false,$decimalPoints='')
    {
        if (empty($decimalPoints)) 
        {
            $decimalPoints = '3';
        }
        if (empty($distanceUnit)) {
            $distanceUnit = 'KM';
        }
        $distanceUnit = strtolower($distanceUnit);
        $pointDifference = $longitudeOne - $longitudeTwo;
        $toSin = (sin(deg2rad($latitudeOne)) * sin(deg2rad($latitudeTwo))) + (cos(deg2rad($latitudeOne)) * cos(deg2rad($latitudeTwo)) * cos(deg2rad($pointDifference)));
        $toAcos = acos($toSin);
        $toRad2Deg = rad2deg($toAcos);

        $toMiles  =  $toRad2Deg * 60 * 1.1515;
        $toKilometers = $toMiles * 1.609344;
        $toNauticalMiles = $toMiles * 0.8684;
        $toMeters = $toKilometers * 1000;
        $toFeets = $toMiles * 5280;
        $toYards = $toFeets / 3;


              switch (strtoupper($distanceUnit)) 
              {
                  case 'ML'://miles
                         $toMiles  = ($round == true ? round($toMiles) : round($toMiles, $decimalPoints));
                         return $toMiles;
                      break;
                  case 'KM'://Kilometers
                        $toKilometers  = ($round == true ? round($toKilometers) : round($toKilometers, $decimalPoints));
                        return $toKilometers;
                      break;
                  case 'MT'://Meters
                        $toMeters  = ($round == true ? round($toMeters) : round($toMeters, $decimalPoints));
                        return $toMeters;
                      break;
                  case 'FT'://feets
                        $toFeets  = ($round == true ? round($toFeets) : round($toFeets, $decimalPoints));
                        return $toFeets;
                      break;
                  case 'YD'://yards
                        $toYards  = ($round == true ? round($toYards) : round($toYards, $decimalPoints));
                        return $toYards;
                      break;
                  case 'NM'://Nautical miles
                        $toNauticalMiles  = ($round == true ? round($toNauticalMiles) : round($toNauticalMiles, $decimalPoints));
                        return $toNauticalMiles;
                      break;
              }


    }

Then use the fucntion as

echo calculateDistanceBetweenTwoPoints('11.657740','77.766270','11.074820','77.002160','ML',true,5);

Hope it helps

Vookles answered 19/11, 2018 at 4:54 Comment(2)
verified with real scenario perfect work in my case.Angelineangelique
Took nearly 5 hours to write it and verify it in real scenarioVookles
F
3

Try this gives awesome results

function getDistance($point1_lat, $point1_long, $point2_lat, $point2_long, $unit = 'km', $decimals = 2) {
        // Calculate the distance in degrees
        $degrees = rad2deg(acos((sin(deg2rad($point1_lat))*sin(deg2rad($point2_lat))) + (cos(deg2rad($point1_lat))*cos(deg2rad($point2_lat))*cos(deg2rad($point1_long-$point2_long)))));

        // Convert the distance in degrees to the chosen unit (kilometres, miles or nautical miles)
        switch($unit) {
            case 'km':
                $distance = $degrees * 111.13384; // 1 degree = 111.13384 km, based on the average diameter of the Earth (12,735 km)
                break;
            case 'mi':
                $distance = $degrees * 69.05482; // 1 degree = 69.05482 miles, based on the average diameter of the Earth (7,913.1 miles)
                break;
            case 'nmi':
                $distance =  $degrees * 59.97662; // 1 degree = 59.97662 nautic miles, based on the average diameter of the Earth (6,876.3 nautical miles)
        }
        return round($distance, $decimals);
    }
Fayth answered 23/11, 2016 at 9:19 Comment(0)
T
2

For exact values do it like that:

public function DistAB()
{
      $delta_lat = $this->lat_b - $this->lat_a ;
      $delta_lon = $this->lon_b - $this->lon_a ;

      $a = pow(sin($delta_lat/2), 2);
      $a += cos(deg2rad($this->lat_a9)) * cos(deg2rad($this->lat_b9)) * pow(sin(deg2rad($delta_lon/29)), 2);
      $c = 2 * atan2(sqrt($a), sqrt(1-$a));

      $distance = 2 * $earth_radius * $c;
      $distance = round($distance, 4);

      $this->measure = $distance;
}

Hmm I think that should do it...

Edit:

For formulars and at least JS-implementations try: http://www.movable-type.co.uk/scripts/latlong.html

Dare me... I forgot to deg2rad all the values in the circle-functions...

Tonnie answered 7/4, 2012 at 10:28 Comment(1)
Thanks for your answer. I've checked this implementation with a simple calculation between pointA(42,12) and pointB(43,12) using $earth_radius = 6372.795477598 I get as result 12745.591 when it should be something around 110,94Simian
D
1

The multiplier is changed at every coordinate because of the great circle distance theory as written here :

http://en.wikipedia.org/wiki/Great-circle_distance

and you can calculate the nearest value using this formula described here:

http://en.wikipedia.org/wiki/Great-circle_distance#Worked_example

the key is converting each degree - minute - second value to all degree value:

N 36°7.2', W 86°40.2'  N = (+) , W = (-), S = (-), E = (+) 
referencing the Greenwich meridian and Equator parallel

(phi)     36.12° = 36° + 7.2'/60' 

(lambda)  -86.67° = 86° + 40.2'/60'
Deepen answered 7/4, 2012 at 10:38 Comment(0)
C
1

One of the easiest ways is:

$my_latitude = "";
$my_longitude = "";
$her_latitude = "";
$her_longitude = "";

$distance = round((((acos(sin(($my_latitude*pi()/180)) * sin(($her_latitude*pi()/180))+cos(($my_latitude*pi()/180)) * cos(($her_latitude*pi()/180)) * cos((($my_longitude- $her_longitude)*pi()/180))))*180/pi())*60*1.1515*1.609344), 2);
echo $distance;

It will round off up to 2 decimal points.

Complect answered 14/3, 2020 at 9:40 Comment(0)
A
1

You can try the library geo-math-php

composer require rkondratuk/geo-math-php:^1

Example:

<?php

use PhpGeoMath\Model\Polar3dPoint;

$polarPoint1 = new Polar3dPoint(
    40.758742779050706, -73.97855507715238, Polar3dPoint::EARTH_RADIUS_IN_METERS
);

$polarPoint2 = new Polar3dPoint(
    40.74843388072615, -73.98566565776102, Polar3dPoint::EARTH_RADIUS_IN_METERS
);

$geoDistance = $polarPoint2->calcGeoDistanceToPoint($polarPoint1);
Archean answered 14/10, 2022 at 22:21 Comment(0)
C
0

Hello here Code For Get Distance and Time Using Two Different Lat and Long

$url ="https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=16.538048,80.613266&destinations=23.0225,72.5714";



    $ch = curl_init();
    // Disable SSL verification

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // Will return the response, if false it print the response
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Set the url
    curl_setopt($ch, CURLOPT_URL,$url);
    // Execute
    $result=curl_exec($ch);
    // Closing
    curl_close($ch);

    $result_array=json_decode($result);
print_r($result_array);

You can check Example Below Link get time between two different locations using latitude and longitude in php

Chaldron answered 19/7, 2016 at 7:6 Comment(1)
It might be unnecessary to call an api for something that can quite simply be found by using math.Belsen

© 2022 - 2024 — McMap. All rights reserved.