mapUtil.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. $url = "https://apis.map.qq.com/ws/distance/v1/matrix/?mode=driving&from={$fromLat},{$fromLnt}&to={$toLat},{$toLnt}&key=" . self::$thirdMapKey;
  28. $curl = new curl\Curl();
  29. $result = $curl->get($url);
  30. $result = Json::decode($result);
  31. $distance = 0;
  32. if (isset($result['status']) && $result['status'] == 0) {
  33. $distance = isset($result['result']['rows'][0]['elements'][0]['distance']) ? $result['result']['rows'][0]['elements'][0]['distance'] : 0;
  34. }
  35. return $distance;
  36. }
  37. //逆地址解析,根据经纬度解析出中文地址 ssh
  38. public static function resolveLocation($lat, $long)
  39. {
  40. $url = "https://apis.map.qq.com/ws/geocoder/v1/?location={$lat},{$long}&get_poi=0&key=" . self::$thirdMapKey;
  41. $curl = new curl\Curl();
  42. $result = $curl->get($url);
  43. $result = Json::decode($result);
  44. return $result;
  45. }
  46. }