Question

I calculate distance between locations using havershine formula.

$distance = (((acos(sin(($fromLat * pi() / 180)) * sin(($toLat * pi( )/ 180)) + cos(($fromLat * pi() / 180)) * cos(($toLat * pi() / 180)) * cos((($fromLon - $toLon) * pi() / 180)))) * 180 / pi()) * 60 * 1.1515);

It works normally, but for some coordinate values it is giving NAN.

Values it gives NAN for are

fromLat : 22.303894
fromLon : 70.802162
toLat : 22.303894
toLon : 70.802162
Was it helpful?

Solution

You're comparing the same coordinates. The acos function is being asked to calculate the arc cosine of 1, which fails. You will want to check if the coordinates are identical before passing it through the function. Also, if you're not getting a number, the result is probably no distance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top