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
有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top