0

I am calculating radius using this code :

function calculateRadius($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 round($miles,2);
  }
}

$radius=calculateRadius($lat1,$long1,$lat2,$long2,'K');

Now if i'm passing the same location to both the lat long parameter, then it is still returning 9.4930730546311E-5 as radius.

user3653474
  • 3,393
  • 6
  • 49
  • 135
  • Can you tell us what your input values are and what you expect as an output? This will help diagnosing the problem a lot – JensV Jul 08 '20 at 11:31
  • This function works well. What values do you set for input? – Majid Tabibpour Jul 08 '20 at 11:35
  • @JensV: Latitude - 28.5355161,longitude: 77.3910265 entering this location in both location – user3653474 Jul 08 '20 at 11:42
  • It should return 0 km – user3653474 Jul 08 '20 at 11:45
  • 1
    You’re aware that `9.4930730546311E-5` _is_ “almost zero”, right? – CBroe Jul 08 '20 at 11:49
  • @CBroe: Yes but how will i compare the distance like if ($distance < $radius) , how to convert this value so that i can easily compare it – user3653474 Jul 08 '20 at 11:52
  • I'm not sure exactly what's wrong with your formula but testing with either of the two solutions from this answer yields the expected result: https://stackoverflow.com/a/10054282/2232127 – JensV Jul 08 '20 at 11:53
  • 1
    You don’t need to “convert” this into anything. Internally, you still have a proper float value here, so you can do your comparisons the absolute normal, boring, standard way. The `…E-5` thing is just _display formatting_ of such tiny values. – CBroe Jul 08 '20 at 11:58

0 Answers0