mapUtil.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * User: ssh
  4. * Date: 2019/11/20
  5. * Time: 23:21
  6. */
  7. namespace common\components;
  8. use linslin\yii2\curl;
  9. use yii\helpers\Json;
  10. class mapUtil
  11. {
  12. public static $thirdMapKey = 'd4ab169335937fb56581ba15ea1bd1a1';
  13. public static function suggestion($region, $keyword)
  14. {
  15. if (getenv('YII_ENV') == 'production') {
  16. $url = "https://restapi.amap.com/v3/assistant/inputtips?city={$region}&keywords={$keyword}&key=" . self::$thirdMapKey;
  17. } else {
  18. $url = "http://restapi.amap.com/v3/assistant/inputtips?city={$region}&keywords={$keyword}&key=" . self::$thirdMapKey;
  19. }
  20. $curl = new curl\Curl();
  21. $result = $curl->get($url);
  22. return Json::decode($result);
  23. }
  24. //计算距离 lng经度 lat纬度 ssh 2020.5.14
  25. public static function calculateDistance($fromLnt, $fromLat, $toLnt, $toLat)
  26. {
  27. if (getenv('YII_ENV') == 'production') {
  28. $url = "https://restapi.amap.com/v5/direction/electrobike?key=" . self::$thirdMapKey . '&origin=' . $fromLnt . ',' . $fromLat . '&destination=' . $toLnt . ',' . $toLat;
  29. } else {
  30. $url = "http://restapi.amap.com/v5/direction/electrobike?key=" . self::$thirdMapKey . '&origin=' . $fromLnt . ',' . $fromLat . '&destination=' . $toLnt . ',' . $toLat;
  31. }
  32. $curl = new curl\Curl();
  33. $result = $curl->get($url);
  34. $result = Json::decode($result);
  35. $distance = 0;
  36. if (!empty($result['status']) && $result['status'] == 1) {
  37. $path = $result['route']['paths'] ?? [];
  38. $current = $path[0] ?? [];
  39. $distance = $current['distance'] ?? 0;
  40. }
  41. return $distance;
  42. }
  43. /**
  44. * 批量计算多个起点到一个终点的距离 (高德 V3 距离测量接口)
  45. * @param array $origins 起点坐标数组,例如:[['lng' => '116.481028', 'lat' => '39.989643'], ...]
  46. * @param string $toLnt 终点经度
  47. * @param string $toLat 终点纬度
  48. * @param int $type 路径计算的方式和方法 (0:直线距离, 1:驾车, 3:步行)
  49. * @return array
  50. */
  51. public static function calculateBatchDistance($origins, $toLnt, $toLat, $type = 1)
  52. {
  53. if (empty($origins)) {
  54. return [];
  55. }
  56. // 将起点数组拼接成高德要求的格式: lng1,lat1|lng2,lat2
  57. $originStrs = [];
  58. foreach ($origins as $origin) {
  59. $originStrs[] = $origin['lng'] . ',' . $origin['lat'];
  60. }
  61. $originsParam = implode('|', $originStrs);
  62. $destinationParam = $toLnt . ',' . $toLat;
  63. $domain = getenv('YII_ENV') == 'production' ? 'https://restapi.amap.com' : 'http://restapi.amap.com';
  64. $url = $domain . "/v3/distance?key=" . self::$thirdMapKey . "&origins={$originsParam}&destination={$destinationParam}&type={$type}";
  65. $curl = new curl\Curl();
  66. $result = $curl->get($url);
  67. $result = \yii\helpers\Json::decode($result);
  68. $distances = [];
  69. if (!empty($result['status']) && $result['status'] == 1 && !empty($result['results'])) {
  70. foreach ($result['results'] as $index => $res) {
  71. // 返回的结果顺序与传入的 origins 顺序一致
  72. $distances[$index] = $res['distance'] ?? 0;
  73. }
  74. }else{
  75. noticeUtil::push("高德批量测距接口返回失败:".json_encode($result)." url:".$url);
  76. }
  77. return $distances;
  78. }
  79. //逆地址解析,根据经纬度解析出中文地址 ssh
  80. public static function resolveLocation($lat, $long)
  81. {
  82. $url = "https://apis.map.qq.com/ws/geocoder/v1/?location={$lat},{$long}&get_poi=0&key=" . self::$thirdMapKey;
  83. $curl = new curl\Curl();
  84. $result = $curl->get($url);
  85. $result = Json::decode($result);
  86. return $result;
  87. }
  88. /**
  89. * 坐标系转换:从 WGS84 转换为 GCJ-02
  90. * @param $lng
  91. * @param $lat
  92. * @return array
  93. */
  94. public static function wgs84ToGcj02($lng, $lat)
  95. {
  96. $lng = (float)$lng;
  97. $lat = (float)$lat;
  98. if (self::outOfChina($lng, $lat)) {
  99. return ['lng' => $lng, 'lat' => $lat];
  100. }
  101. $a = 6378245.0;
  102. $ee = 0.00669342162296594323;
  103. $dLat = self::transformLat($lng - 105.0, $lat - 35.0);
  104. $dLng = self::transformLng($lng - 105.0, $lat - 35.0);
  105. $radLat = $lat / 180.0 * M_PI;
  106. $magic = sin($radLat);
  107. $magic = 1 - $ee * $magic * $magic;
  108. $sqrtMagic = sqrt($magic);
  109. $dLat = ($dLat * 180.0) / (($a * (1 - $ee)) / ($magic * $sqrtMagic) * M_PI);
  110. $dLng = ($dLng * 180.0) / ($a / $sqrtMagic * cos($radLat) * M_PI);
  111. return ['lng' => $lng + $dLng, 'lat' => $lat + $dLat];
  112. }
  113. private static function transformLat($lng, $lat)
  114. {
  115. $ret = -100.0 + 2.0 * $lng + 3.0 * $lat + 0.2 * $lat * $lat + 0.1 * $lng * $lat + 0.2 * sqrt(abs($lng));
  116. $ret += (20.0 * sin(6.0 * $lng * M_PI) + 20.0 * sin(2.0 * $lng * M_PI)) * 2.0 / 3.0;
  117. $ret += (20.0 * sin($lat * M_PI) + 40.0 * sin($lat / 3.0 * M_PI)) * 2.0 / 3.0;
  118. $ret += (160.0 * sin($lat / 12.0 * M_PI) + 320.0 * sin($lat * M_PI / 30.0)) * 2.0 / 3.0;
  119. return $ret;
  120. }
  121. private static function transformLng($lng, $lat)
  122. {
  123. $ret = 300.0 + $lng + 2.0 * $lat + 0.1 * $lng * $lng + 0.1 * $lng * $lat + 0.1 * sqrt(abs($lng));
  124. $ret += (20.0 * sin(6.0 * $lng * M_PI) + 20.0 * sin(2.0 * $lng * M_PI)) * 2.0 / 3.0;
  125. $ret += (20.0 * sin($lng * M_PI) + 40.0 * sin($lng / 3.0 * M_PI)) * 2.0 / 3.0;
  126. $ret += (150.0 * sin($lng / 12.0 * M_PI) + 300.0 * sin($lng / 30.0 * M_PI)) * 2.0 / 3.0;
  127. return $ret;
  128. }
  129. private static function outOfChina($lng, $lat)
  130. {
  131. if ($lng < 72.004 || $lng > 137.8347) {
  132. return true;
  133. }
  134. if ($lat < 0.8293 || $lat > 55.8271) {
  135. return true;
  136. }
  137. return false;
  138. }
  139. }