ts2000abc Posted March 20, 2009 Share Posted March 20, 2009 Does anyone know if there is a easy way to count distance (in miles or kilometers) between two gps-points? Example points: Berlin, Germany: LAT 52.516074 , LON 13.376987 Stockholm, Sweden: LAT 59.332175 , LON 18.062435 Link to comment https://forums.phpfreaks.com/topic/150365-counting-distance-between-2-gps-points/ Share on other sites More sharing options...
Mark Baker Posted March 20, 2009 Share Posted March 20, 2009 function calculateDistanceFromLatLong($point1,$point2,$uom='km') { // Use Haversine formula to calculate the great circle distance // between two points identified by longitude and latitude switch (strtolower($uom)) { case 'km' : $earthMeanRadius = 6371.009; // km break; case 'm' : $earthMeanRadius = 6371.009 * 1000; // km break; case 'miles' : $earthMeanRadius = 3958.761; // miles break; case 'yards' : case 'yds' : $earthMeanRadius = 3958.761 * 1760; // miles break; case 'feet' : case 'ft' : $earthMeanRadius = 3958.761 * 1760 * 3; // miles break; case 'nm' : $earthMeanRadius = 3440.069; // miles break; } $deltaLatitude = deg2rad($point2['latitude'] - $point1['latitude']); $deltaLongitude = deg2rad($point2['longitude'] - $point1['longitude']); $a = sin($deltaLatitude / 2) * sin($deltaLatitude / 2) + cos(deg2rad($point1['latitude'])) * cos(deg2rad($point2['latitude'])) * sin($deltaLongitude / 2) * sin($deltaLongitude / 2); $c = 2 * atan2(sqrt($a), sqrt(1-$a)); $distance = $earthMeanRadius * $c; return $distance; } // function calculateDistanceFromLatLong() $startPoint = array( 'latitude' => 52.516074, // Berlin, Germany 'longitude' => 13.376987 ); $endPoint = array( 'latitude' => 59.332175, // Stockholm, Sweden 'longitude' => 18.062435 ); $uom = 'km'; $distance = calculateDistanceFromLatLong ($startPoint,$endPoint,$uom); echo $distance.' '.$uom.'<br />'; $uom = 'm'; $distance = calculateDistanceFromLatLong ($startPoint,$endPoint,$uom); echo $distance.' '.$uom.'<br />'; $uom = 'miles'; $distance = calculateDistanceFromLatLong ($startPoint,$endPoint,$uom); echo $distance.' '.$uom.'<br />'; $uom = 'yds'; $distance = calculateDistanceFromLatLong ($startPoint,$endPoint,$uom); echo $distance.' '.$uom.'<br />'; $uom = 'ft'; $distance = calculateDistanceFromLatLong ($startPoint,$endPoint,$uom); echo $distance.' '.$uom.'<br />'; $uom = 'nm'; $distance = calculateDistanceFromLatLong ($startPoint,$endPoint,$uom); echo $distance.' '.$uom.'<br />'; Link to comment https://forums.phpfreaks.com/topic/150365-counting-distance-between-2-gps-points/#findComment-789679 Share on other sites More sharing options...
ts2000abc Posted March 20, 2009 Author Share Posted March 20, 2009 thanks... that code works just fine Link to comment https://forums.phpfreaks.com/topic/150365-counting-distance-between-2-gps-points/#findComment-789709 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.