Heim  >  Artikel  >  Backend-Entwicklung  >  计算地图上两点间的距离

计算地图上两点间的距离

WBOY
WBOYOriginal
2016-07-25 08:50:391093Durchsuche
  1. class GeoHelper
  2. {
  3. /**
  4. * @param int $lat1
  5. * @param int $lon1
  6. * @param int $lat2
  7. * @param int $lon2
  8. * @param string $unit
  9. * @return
  10. */
  11. public static function distance($lat1, $lon1, $lat2, $lon2, $unit = "K")
  12. {
  13. $theta = $lon1 - $lon2;
  14. $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad
  15. ($lat2)) * cos(deg2rad($theta));
  16. $dist = acos($dist);
  17. $dist = rad2deg($dist);
  18. $miles = $dist * 60 * 1.1515;
  19. $unit = strtoupper($unit);
  20. if ($unit == "K") {
  21. return ($miles * 1.609344);
  22. } else
  23. if ($unit == "N") {
  24. return ($miles * 0.8684);
  25. } else { //mi
  26. return $miles;
  27. }
  28. }
  29. /**
  30. *
  31. * @param string $address
  32. * @param string $apikey
  33. * @return array [1]:lat [0]:lng
  34. */
  35. public static function getLatLng($address, $apikey)
  36. {
  37. $find = array("\n", "\r", " ");
  38. $replace = array("", "", "+");
  39. $address = str_replace($find, $replace, $address);
  40. $url = 'http://maps.google.com/maps/geo?q=' . $address . '&key=' . $apikey .
  41. '&sensor=false&output=xml&oe=utf8';
  42. $response = self::xml2array($url);
  43. $coordinates = $response['kml']['Response']['Placemark']['Point']['coordinates'];
  44. if (!empty($coordinates)) {
  45. $point_array = split(",", $coordinates);
  46. return $point_array;
  47. }
  48. }
  49. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:日历表格 Nächster Artikel:6个创建Web应用程序的高效PHP框架