mapUtil.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. }