php - How can I calculate distance travelled for a user's journey? -
in database, have few thousand rows latitude , longitude stores.
in php, how can calculate total distance user has travelled on journey stored in database recorded latitude , longitude points?
is there mathematical equation can reference or something? in system performing calculation on queue , caching user's journey won't change after system has recorded efficiency isn't issue.
i ideally output total of distance travelled in miles.
thanks!
update: here code sample of have done far.
$distance = 0; $earthradius = 6371; for($i = 0; $i < $journey->data->count(); $i++) { $currentdata = $journey->data->get($i); $previousdata = $journey->data->get($i-1); if($previousdata == null) continue; $dlat = deg2rad($currentdata->latitude - $previousdata->latitude); $dlon = deg2rad($currentdata->longitude - $previousdata->longitude); $a = sin($dlat / 2) * sin($dlat / 2) + cos(deg2rad($currentdata->latitude)) * cos(deg2rad($previousdata->latitude)) * sin($dlon / 2) * sin($dlon / 2); $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); $distance += $earthradius * $c; } // $distance should total in kilometers?
is correct?
Comments
Post a Comment