mapUtil.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. 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. }
  75. return $distances;
  76. }
  77. //逆地址解析,根据经纬度解析出中文地址 ssh
  78. public static function resolveLocation($lat, $long)
  79. {
  80. $url = "https://apis.map.qq.com/ws/geocoder/v1/?location={$lat},{$long}&get_poi=0&key=" . self::$thirdMapKey;
  81. $curl = new curl\Curl();
  82. $result = $curl->get($url);
  83. $result = Json::decode($result);
  84. return $result;
  85. }
  86. /**
  87. * 坐标系转换:从 WGS84 转换为 GCJ-02
  88. * @param $lng
  89. * @param $lat
  90. * @return array
  91. */
  92. public static function wgs84ToGcj02($lng, $lat)
  93. {
  94. $lng = (float)$lng;
  95. $lat = (float)$lat;
  96. if (self::outOfChina($lng, $lat)) {
  97. return ['lng' => $lng, 'lat' => $lat];
  98. }
  99. $a = 6378245.0;
  100. $ee = 0.00669342162296594323;
  101. $dLat = self::transformLat($lng - 105.0, $lat - 35.0);
  102. $dLng = self::transformLng($lng - 105.0, $lat - 35.0);
  103. $radLat = $lat / 180.0 * M_PI;
  104. $magic = sin($radLat);
  105. $magic = 1 - $ee * $magic * $magic;
  106. $sqrtMagic = sqrt($magic);
  107. $dLat = ($dLat * 180.0) / (($a * (1 - $ee)) / ($magic * $sqrtMagic) * M_PI);
  108. $dLng = ($dLng * 180.0) / ($a / $sqrtMagic * cos($radLat) * M_PI);
  109. return ['lng' => $lng + $dLng, 'lat' => $lat + $dLat];
  110. }
  111. private static function transformLat($lng, $lat)
  112. {
  113. $ret = -100.0 + 2.0 * $lng + 3.0 * $lat + 0.2 * $lat * $lat + 0.1 * $lng * $lat + 0.2 * sqrt(abs($lng));
  114. $ret += (20.0 * sin(6.0 * $lng * M_PI) + 20.0 * sin(2.0 * $lng * M_PI)) * 2.0 / 3.0;
  115. $ret += (20.0 * sin($lat * M_PI) + 40.0 * sin($lat / 3.0 * M_PI)) * 2.0 / 3.0;
  116. $ret += (160.0 * sin($lat / 12.0 * M_PI) + 320.0 * sin($lat * M_PI / 30.0)) * 2.0 / 3.0;
  117. return $ret;
  118. }
  119. private static function transformLng($lng, $lat)
  120. {
  121. $ret = 300.0 + $lng + 2.0 * $lat + 0.1 * $lng * $lng + 0.1 * $lng * $lat + 0.1 * sqrt(abs($lng));
  122. $ret += (20.0 * sin(6.0 * $lng * M_PI) + 20.0 * sin(2.0 * $lng * M_PI)) * 2.0 / 3.0;
  123. $ret += (20.0 * sin($lng * M_PI) + 40.0 * sin($lng / 3.0 * M_PI)) * 2.0 / 3.0;
  124. $ret += (150.0 * sin($lng / 12.0 * M_PI) + 300.0 * sin($lng / 30.0 * M_PI)) * 2.0 / 3.0;
  125. return $ret;
  126. }
  127. private static function outOfChina($lng, $lat)
  128. {
  129. if ($lng < 72.004 || $lng > 137.8347) {
  130. return true;
  131. }
  132. if ($lat < 0.8293 || $lat > 55.8271) {
  133. return true;
  134. }
  135. return false;
  136. }
  137. }