mapUtil.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 = 'fe792548aa6e15ef4d5d501e508eaeae';
  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. noticeUtil::push("执行到这里:xxx");
  54. if (empty($origins)) {
  55. return [];
  56. }
  57. // 将起点数组拼接成高德要求的格式: lng1,lat1|lng2,lat2
  58. $originStrs = [];
  59. foreach ($origins as $origin) {
  60. $originStrs[] = $origin['lng'] . ',' . $origin['lat'];
  61. }
  62. $originsParam = implode('|', $originStrs);
  63. $destinationParam = $toLnt . ',' . $toLat;
  64. $domain = getenv('YII_ENV') == 'production' ? 'https://restapi.amap.com' : 'http://restapi.amap.com';
  65. $url = $domain . "/v3/distance?key=" . self::$thirdMapKey . "&origins={$originsParam}&destination={$destinationParam}&type={$type}";
  66. $curl = new curl\Curl();
  67. $result = $curl->get($url);
  68. $result = \yii\helpers\Json::decode($result);
  69. noticeUtil::push("执行到这里:".json_encode($result)." url:".$url);
  70. $distances = [];
  71. if (!empty($result['status']) && $result['status'] == 1 && !empty($result['results'])) {
  72. foreach ($result['results'] as $index => $res) {
  73. // 返回的结果顺序与传入的 origins 顺序一致
  74. $distances[$index] = $res['distance'] ?? 0;
  75. }
  76. }else{
  77. noticeUtil::push("高德批量测距接口返回失败:".json_encode($result)." url:".$url);
  78. }
  79. return $distances;
  80. }
  81. //逆地址解析,根据经纬度解析出中文地址 ssh
  82. public static function resolveLocation($lat, $long)
  83. {
  84. $url = "https://apis.map.qq.com/ws/geocoder/v1/?location={$lat},{$long}&get_poi=0&key=" . self::$thirdMapKey;
  85. $curl = new curl\Curl();
  86. $result = $curl->get($url);
  87. $result = Json::decode($result);
  88. return $result;
  89. }
  90. /**
  91. * 坐标系转换:从 WGS84 转换为 GCJ-02
  92. * @param $lng
  93. * @param $lat
  94. * @return array
  95. */
  96. public static function wgs84ToGcj02($lng, $lat)
  97. {
  98. $lng = (float)$lng;
  99. $lat = (float)$lat;
  100. if (self::outOfChina($lng, $lat)) {
  101. return ['lng' => $lng, 'lat' => $lat];
  102. }
  103. $a = 6378245.0;
  104. $ee = 0.00669342162296594323;
  105. $dLat = self::transformLat($lng - 105.0, $lat - 35.0);
  106. $dLng = self::transformLng($lng - 105.0, $lat - 35.0);
  107. $radLat = $lat / 180.0 * M_PI;
  108. $magic = sin($radLat);
  109. $magic = 1 - $ee * $magic * $magic;
  110. $sqrtMagic = sqrt($magic);
  111. $dLat = ($dLat * 180.0) / (($a * (1 - $ee)) / ($magic * $sqrtMagic) * M_PI);
  112. $dLng = ($dLng * 180.0) / ($a / $sqrtMagic * cos($radLat) * M_PI);
  113. return ['lng' => $lng + $dLng, 'lat' => $lat + $dLat];
  114. }
  115. private static function transformLat($lng, $lat)
  116. {
  117. $ret = -100.0 + 2.0 * $lng + 3.0 * $lat + 0.2 * $lat * $lat + 0.1 * $lng * $lat + 0.2 * sqrt(abs($lng));
  118. $ret += (20.0 * sin(6.0 * $lng * M_PI) + 20.0 * sin(2.0 * $lng * M_PI)) * 2.0 / 3.0;
  119. $ret += (20.0 * sin($lat * M_PI) + 40.0 * sin($lat / 3.0 * M_PI)) * 2.0 / 3.0;
  120. $ret += (160.0 * sin($lat / 12.0 * M_PI) + 320.0 * sin($lat * M_PI / 30.0)) * 2.0 / 3.0;
  121. return $ret;
  122. }
  123. private static function transformLng($lng, $lat)
  124. {
  125. $ret = 300.0 + $lng + 2.0 * $lat + 0.1 * $lng * $lng + 0.1 * $lng * $lat + 0.1 * sqrt(abs($lng));
  126. $ret += (20.0 * sin(6.0 * $lng * M_PI) + 20.0 * sin(2.0 * $lng * M_PI)) * 2.0 / 3.0;
  127. $ret += (20.0 * sin($lng * M_PI) + 40.0 * sin($lng / 3.0 * M_PI)) * 2.0 / 3.0;
  128. $ret += (150.0 * sin($lng / 12.0 * M_PI) + 300.0 * sin($lng / 30.0 * M_PI)) * 2.0 / 3.0;
  129. return $ret;
  130. }
  131. private static function outOfChina($lng, $lat)
  132. {
  133. if ($lng < 72.004 || $lng > 137.8347) {
  134. return true;
  135. }
  136. if ($lat < 0.8293 || $lat > 55.8271) {
  137. return true;
  138. }
  139. return false;
  140. }
  141. }