How to check if a certain coordinates fall to another coordinates radius using PHP only
Asked Answered
O

5

25

I have seen so many functions but it happens to work only for MySQL or Postgresql. I want the equivalent logic for PHP. I'm doing some comparisons, like I have this data that were being produced when created.

Lat: 56.130366
Long: -106.34677099999

Later on, I want to check if this coordinates will fall within a radius of another coordinates.

Lat: 57.223366
Long: -106.34675644699
radius: 100000 ( meters )

Thanks in advance!

Oxfordshire answered 15/9, 2012 at 18:3 Comment(1)
Google "Haversine formula" or "Vincenty formula" to work out the distance between two lat/long positionsLareine
O
56

Thanks for the help. Below is an example function that takes two sets of longitude and latitude co-ordinates and returns the distance between the two.

function getDistance($latitude1, $longitude1, $latitude2, $longitude2) {  
  $earth_radius = 6371;

  $dLat = deg2rad($latitude2 - $latitude1);  
  $dLon = deg2rad($longitude2 - $longitude1);  

  $a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon/2) * sin($dLon/2);  
  $c = 2 * asin(sqrt($a));  
  $d = $earth_radius * $c;  

  return $d;  
}

$distance = getDistance(56.130366, -106.34677099999, 57.223366, -106.34675644699);
if ($distance < 100) {
  echo "Within 100 kilometer radius";
} else {
  echo "Outside 100 kilometer radius";
}
Oxfordshire answered 15/9, 2012 at 18:35 Comment(3)
The equivalent, but in Javascript because I spent way too long looking for this.Zingale
@AamirR true that but it was totally different. I modified it and posted it as an answer. Why now? Its already been 5 years, move on buddy lolOxfordshire
why earth_radius 6371?Champollion
F
9

You should use Haversine formula to compute distance between two points. You have a PHP version here.

Then just check if distance < 100000.

Frustum answered 15/9, 2012 at 18:10 Comment(4)
Good codes you got there. But the distance is really in meters or kilometers?Oxfordshire
It depends on the earth radius. If you use 6371 like in the code I have linked, it is kilometers :)Frustum
I saw some algorithms using 6367 as the earth radius but most of them are 6371 does it really matters or not? anyway thanks :)Oxfordshire
What radius do I have to use to get the distance in meters? 6371000?Overcheck
Z
1

This should help,

$lat_origin = 56.130366;
$long_origin = -106.34677099999;

$lat_dest = 57.223366;
$long_dest = -106.34675644699;

$radius      = 3958;      # Earth's radius (miles, convert to meters)
$deg_per_rad = 57.29578;  # Number of degrees/radian (for conversion)

$distance = ($radius * pi() * sqrt(
            ($lat_origin - $lat_dest)
            * ($lat_origin - $lat_dest)
            + cos($lat_origin / $deg_per_rad)  # Convert these to
            * cos($lat_dest / $deg_per_rad)    # radians for cos()
            * ($long_origin - $long_dest)
            * ($long_origin - $long_dest)
    ) / 180);
Zimmerman answered 15/9, 2012 at 18:11 Comment(1)
What is the purpose of $deg_per_rad here?Quotient
L
1
//  Vincenty formula to calculate great circle distance between 2 locations
//      expressed as Lat/Long in KM 

function VincentyDistance($lat1,$lat2,$lon1,$lon2){ 
    $a = 6378137 - 21 * sin(lat); 
    $b = 6356752.3142; 
    $f = 1/298.257223563; 

    $p1_lat = $lat1/57.29577951; 
    $p2_lat = $lat2/57.29577951; 
    $p1_lon = $lon1/57.29577951; 
    $p2_lon = $lon2/57.29577951; 

    $L = $p2_lon - $p1_lon; 

    $U1 = atan((1-$f) * tan($p1_lat)); 
    $U2 = atan((1-$f) * tan($p2_lat)); 

    $sinU1 = sin($U1); 
    $cosU1 = cos($U1); 
    $sinU2 = sin($U2); 
    $cosU2 = cos($U2); 

    $lambda = $L; 
    $lambdaP = 2*PI; 
    $iterLimit = 20; 

    while(abs($lambda-$lambdaP) > 1e-12 && $iterLimit>0) { 
        $sinLambda = sin($lambda); 
        $cosLambda = cos($lambda); 
        $sinSigma = sqrt(($cosU2*$sinLambda) * ($cosU2*$sinLambda) + ($cosU1*$sinU2-$sinU1*$cosU2*$cosLambda) * ($cosU1*$sinU2-$sinU1*$cosU2*$cosLambda)); 

        //if ($sinSigma==0){return 0;}  // co-incident points 
        $cosSigma = $sinU1*$sinU2 + $cosU1*$cosU2*$cosLambda; 
        $sigma = atan2($sinSigma, $cosSigma); 
        $alpha = asin($cosU1 * $cosU2 * $sinLambda / $sinSigma); 
        $cosSqAlpha = cos($alpha) * cos($alpha); 
        $cos2SigmaM = $cosSigma - 2*$sinU1*$sinU2/$cosSqAlpha; 
        $C = $f/16*$cosSqAlpha*(4+$f*(4-3*$cosSqAlpha)); 
        $lambdaP = $lambda; 
        $lambda = $L + (1-$C) * $f * sin($alpha) * ($sigma + $C*$sinSigma*($cos2SigmaM+$C*$cosSigma*(-1+2*$cos2SigmaM*$cos2SigmaM))); 
    } 

    $uSq = $cosSqAlpha*($a*$a-$b*$b)/($b*$b); 
    $A = 1 + $uSq/16384*(4096+$uSq*(-768+$uSq*(320-175*$uSq))); 
    $B = $uSq/1024 * (256+$uSq*(-128+$uSq*(74-47*$uSq))); 

    $deltaSigma = $B*$sinSigma*($cos2SigmaM+$B/4*($cosSigma*(-1+2*$cos2SigmaM*$cos2SigmaM)- $B/6*$cos2SigmaM*(-3+4*$sinSigma*$sinSigma)*(-3+4*$cos2SigmaM*$cos2SigmaM))); 

    $s = $b*$A*($sigma-$deltaSigma); 
    return $s/1000; 
} 


echo VincentyDistance($lat1,$lat2,$lon1,$lon2); 
Lareine answered 15/9, 2012 at 18:11 Comment(0)
A
1

This function takes two sets of latitude, longitude and gives the distance between the two in the specified Unit.

function distance($lat1, $lon1, $lat2, $lon2, $unit) {
  if (($lat1 == $lat2) && ($lon1 == $lon2)) {
    return 0;
  } else {
    $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;
    }
  }
}

Source: https://www.geodatasource.com/developers/php

Usage:

distance(32.9697, -96.80322, 29.46786, -98.53506, "M");

The last argument is the Unit of distance. Possible Units:

  • M for Miles
  • K for Kilometers
  • N for Nautical Miles
Anchusin answered 20/3, 2021 at 8:9 Comment(1)
This could be improved a little by adding an epsilon value for checking if two points are "close enough" rather than exact same float values. Replace the lat1 === lat2 conditional with something like: if (abs($lat1 - $lat2) < 0.02 && abs($long1 - $long2) < 0.02) return 0;Scientistic

© 2022 - 2024 — McMap. All rights reserved.