0

I am calculating distance between various latitudes and longitudes from the database. I am using this formula for calculation:

for($i=0;$i<sizeof($latArray)-1;$i++)
{
  //$dis + =    distance($latArray[$i], $langArray[$i], $latArray[$i+1], $langArray[$i+1], "K");  
  $theta = $langArray[$i] - $langArray[$i+1];

   echo "long :".$langArray[$i];
  echo "lat:".$latArray[$i];
  echo "<br>";


  echo "long :".$langArray[$i+1];
  echo "lat:".$latArray[$i+1]; 

  $dist = sin(deg2rad($latArray[$i])) * sin(deg2rad($latArray[$i+1])) +  cos(deg2rad($latArray[$i])) * cos(deg2rad($latArray[$i+1])) * cos(deg2rad($theta));
 // echo $dist;
  $dist = acos($dist);
  $dist = rad2deg($dist);
  /* $miles = $dist * 60 * 1.1515;

  $discalc = $miles * 1.609344; */
  $distance = $dist * 111.13384;
 // echo "Distance :". number_format((float)$discalc, 3, '.', '')."<br><br>";

  $total = $total+$distance ;  

}

 echo "Total Distance :". number_format((float)$total, 3, '.', '')." Kilometers" ;

After sum of distances it is showing nan kilometers. Why this happening .

heema
  • 29
  • 1
  • 6
  • @hemma, please update your code with some actual "data". What are the values of `$latArray`, `$theta` ? – JustBaron Feb 20 '17 at 10:20
  • i have edited the question. – heema Feb 20 '17 at 10:22
  • 2
    Mate, if we don't have any figures, how are we supposed to help you? You are providing us a calculation without any data. – JustBaron Feb 20 '17 at 10:24
  • these data are coming from database. I have 3800 rows for latitudes and longitudes. – heema Feb 20 '17 at 10:26
  • Are all data giving NaN? Give at least one example of data that produce NaN. – Toto Feb 20 '17 at 10:35
  • when lat , lng comes 28.54432,77.401545 and lat lng 28.5443267,77.4015317... At this time giving me nan kilometeres – heema Feb 20 '17 at 10:43
  • With these data, I get: `Total Distance :0.002 Kilometers` You should have other things that give error. Are you sure you have **exactly** these data in the arrays? – Toto Feb 20 '17 at 11:14

0 Answers0