ホームページ  >  記事  >  バックエンド開発  >  地図上の 2 点間の距離を計算します

地図上の 2 点間の距離を計算します

WBOY
WBOYオリジナル
2016-07-25 08:50:391092ブラウズ
  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=' 。 $アドレス . '&key=' 。 $apikey .
  41. '&sensor=false&output=xml&oe=utf8';
  42. $response = self::xml2array($url);
  43. $座標 = $response['kml']['Response']['Placemark'][' Point']['座標'];
  44. if (!empty($座標)) {
  45. $point_array = Split(",", $座標);
  46. return $point_array;
  47. }
  48. }
  49. }
复制代コード


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。