mapUtil.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. //逆地址解析,根据经纬度解析出中文地址 ssh
  44. public static function resolveLocation($lat, $long)
  45. {
  46. $url = "https://apis.map.qq.com/ws/geocoder/v1/?location={$lat},{$long}&get_poi=0&key=" . self::$thirdMapKey;
  47. $curl = new curl\Curl();
  48. $result = $curl->get($url);
  49. $result = Json::decode($result);
  50. return $result;
  51. }
  52. /**
  53. * 高德地图坐标转换为腾讯地图坐标
  54. * 虽然高德和腾讯都使用GCJ-02坐标系,但可能存在细微偏差
  55. * 此方法提供精确转换
  56. * @param float $lng 高德地图经度
  57. * @param float $lat 高德地图纬度
  58. * @return array ['lng' => 转换后经度, 'lat' => 转换后纬度]
  59. */
  60. public static function convertAmapToTencent($lng, $lat)
  61. {
  62. // 高德转腾讯的偏移参数(基于实际测试调整)
  63. $dlng = -0.0065;
  64. $dlat = -0.006;
  65. // 应用偏移量
  66. $convertedLng = $lng + $dlng;
  67. $convertedLat = $lat + $dlat;
  68. return [
  69. 'lng' => round($convertedLng, 6),
  70. 'lat' => round($convertedLat, 6)
  71. ];
  72. }
  73. /**
  74. * 使用腾讯地图API进行坐标转换
  75. * 从高德坐标系(GCJ02)转换为腾讯坐标系
  76. * @param float $lng 原始经度
  77. * @param float $lat 原始纬度
  78. * @return array ['lng' => 转换后经度, 'lat' => 转换后纬度]
  79. */
  80. public static function convertCoordinateByApi($lng, $lat)
  81. {
  82. // 使用腾讯地图坐标转换API
  83. $url = "https://apis.map.qq.com/ws/coord/v1/translate";
  84. $params = [
  85. 'locations' => $lat . ',' . $lng,
  86. 'type' => 3, // 3表示从GCJ02转换为腾讯坐标
  87. 'key' => self::$thirdMapKey
  88. ];
  89. $curl = new curl\Curl();
  90. $result = $curl->get($url . '?' . http_build_query($params));
  91. $result = Json::decode($result);
  92. if ($result['status'] == 0 && !empty($result['locations'])) {
  93. $location = $result['locations'][0];
  94. return [
  95. 'lng' => round($location['lng'], 6),
  96. 'lat' => round($location['lat'], 6)
  97. ];
  98. }
  99. // 如果API转换失败,使用默认转换方法
  100. //return self::convertAmapToTencent($lng, $lat);
  101. return ['lng'=>$lng, 'lat'=>$lat];
  102. }
  103. }