Hi Everyone,

I'm trying to use a math equation ( see bottom of post) to get the distance of two points. I am able to get the data from the user and the server. However, when I try to use the equation, NAN(not a number) is displayed. I tried to resolve this issue by casting the variables to (float) but this did not work either. I'm not sure what is off here. Can you take a look?

	
// get user input
$userLong=(float)$_POST['userLong']=-73.9599609375;

$userLat=(float)$_POST['userLat']=40.8169272319157;

[/code]

//data was supplied from the server in the while loop.  Use foreach loop to parse data for  getDistance function 

foreach ($_SESSION['events'] as $event) {
   echo $event["long".$i];
   $eventLong=$event["long".$i];
   $eventLat= $event["lat".$i];

  $distance = getDistance($userLat,$eventLat, $userLong, $eventLong);

			echo $distance;


		}// end foreach

// for each loop supplied data use in equation below to get distance. 

function getDistance($lat1,$lat2,$lon1,$lon2){
   $lat1 = (int) $lat1;
   $lat2 = (int) $lat2;
   $lon1 = (int) $lon1;
   $lon2 = (int) $lon2;

   $radians =6371; //km
   $d=acos(sin($lat1) * sin($lat2) + cos($lat1)* cos($lat2)* cos($lon1-$lon2)* $radians);
return $d;

}

    I changed int to float:

    function getDistance($lat1,$lat2,$lon1,$lon2){
       $lat1 = (float) $lat1;
       $lat2 = (float) $lat2;
       $lon1 = (float) $lon1;
       $lon2 = (float) $lon2;
    
       $radians =6371; //km
       $d=acos(sin($lat1) * sin($lat2) + cos($lat1)* cos($lat2)* cos($lon1-$lon2)* $radians);
    return $d;
    
    } 

      Found Carmon Santdiago? Waldo? Michael Westen? NO? But I found the answer!. php function acros() returns NAN if the number is between 1 and infinity. This function only works with fractions ( in this case decimals). I snooped around and found another function on this site http://www.zipcodeworld.com/samples/distance.php.html

      this works fine:

      $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);
      
      return $dist; 
        Write a Reply...