10-30-2018, 01:18 AM
Hi
I have many locations(about 1000 location)
I need calculate distance between this location
My code is :
I calculate distance between two location with foreach
In each loop,i save previous location and get distance from their
But it is wrong
Does anybody can help me?
I have many locations(about 1000 location)
I need calculate distance between this location
My code is :
PHP Code:
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$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);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if (strtolower($unit) == "k") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
I calculate distance between two location with foreach
PHP Code:
foreach($result as $row) {
if ($current != NULL) {
$distance += distance($current['latitude'],$current['longitude'],$row['latitude'],$row['longitude'],'k') ;
$current = $row;
} else {
$current = $row;
}
}
In each loop,i save previous location and get distance from their
But it is wrong
Does anybody can help me?