# Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.39 KB  |  hits: 8  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. PHP and GPS Coordinates, getting the total distance along paths made up of several points
2. \$coordinates = "-82.36554110283872,26.15200821551467,0 -82.420692,26.097404,0 -82.52855700000001,26.186567,0 -82.41250599999999,25.996422,0 -82.50644510379755,26.05431354409091,0"
3.
4. function coordDistance(\$lat1, \$lon1, \$lat2, \$lon2) {
5. \$delta_lat = \$lat2 - \$lat1;
6. \$delta_lon = \$lon2 - \$lon1;
7.
8. \$earth_radius = 20908800.00; //Distance around the earth in feet
9.
10. \$alpha  = \$delta_lat/2;
11. \$beta   = \$delta_lon/2;
13. \$c      = asin(min(1, sqrt(\$a)));
14. \$distance = 2*\$earth_radius * \$c;
15. \$distance = round(\$distance, 4);
16.
17. return \$distance;
18. }
19.
20. \$total = 0;
21.
22. \$points = explode(' ', \$coordinates);
23. \$count = count(\$points);
24.
25. for (\$i = 0; \$count - 1 > \$i; ++\$i) {
26.     list(\$lon1, \$lat1, \$alt1) = explode(',', \$points[\$i]);
27.     list(\$lon2, \$lat2, \$alt2) = explode(',', \$points[\$i + 1]);
28.
29.     \$total += coordDistance(\$lat1, \$lon1, \$lat2, \$lon2);
30. }
31.
32. \$coordinatesArray = explode(" ", \$coordinates);
33.
34. coordDistance(\$coordinatesArray[i], \$coordinatesArray[i+1],); // in a loop
35.
36. \$coordinateA = explode(",", \$coordinatesArray[i]); // again, in a loop
37. \$coordinateB = explode(",", \$coordinatesArray[i+1]);
38.
39. coordDistance(\$coordinateA[0], \$coordinateB[0], \$coordinateA[1], \$coordinateB[1])