Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

251
Views
Check if a latitude and longitude is within a circle in PHP

Original question Check if a latitude and longitude is within a circle

I would like to know how this can be achieved using PHP. I have an array of latitudes and longitudes and would like to test them against another array of latitudes/longitudes to give a true/false scenario

enter image description here

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I've prepared the sample code snippet for you. It's working well for me. Feel free to adapt with your own logic.

$lat1 = "10.5900627";
$lon1 = "77.1933317";

$lat2 = "10.6068507";
$lon2 = "77.117246";

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

if(distance($lat1, $lon1, $lat2, $lon2, "K") < 10)
{
  echo "True";
}
else
{
  echo "False";
}

If the result of 'K' is less than 10Km, then you will get the result as "True". Otherwise 'False' will return as result.

over 4 years ago · Santiago Trujillo Report

0

I would do it this way, first I'll need to compute the distance between two points (this is a method close to the one of Google Maps: https://stackoverflow.com/a/53069194/7071905) :

function computeDistance($lat1, $lng1, $lat2, $lng2, $radius)
{
    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;
}

Then I'll need a simple function with the base point (the center) to return true when the distance is below 10 kilometers ($KM) :

function isWithinCircle($lat, $lng, $KM = 10): bool
{
    $baseLat = 0;
    $baseLng = 0;
    $radius  = 6378137;

    return computeDistance($baseLat, $baseLng, $lat, $lng, $radius) <= $KM;
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!