mapUtil.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. /**
  3. * User: shish
  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 $txMapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  13. //腾讯地图关键词输入提醒 shish 2020.1.21
  14. //https://lbs.qq.com/webservice_v1/guide-suggestion.html
  15. public static function suggestion($region, $keyword)
  16. {
  17. $url = "http://apis.map.qq.com/ws/place/v1/suggestion/?region={$region}&keyword={$keyword}&key=" . self::$txMapKey;
  18. $curl = new curl\Curl();
  19. $result = $curl->get($url);
  20. $result = Json::decode($result);
  21. return $result;
  22. }
  23. //计算距离 lng经度 lat纬度 shish 2020.5.14
  24. public static function calculateDistance($fromLnt, $fromLat, $toLnt, $toLat)
  25. {
  26. $url = "https://apis.map.qq.com/ws/distance/v1/?mode=driving&from={$fromLat},{$fromLnt}&to={$toLat},{$toLnt}&key=" . self::$txMapKey;
  27. $curl = new curl\Curl();
  28. $result = $curl->get($url);
  29. $result = Json::decode($result);
  30. $distance = 0;
  31. if (isset($result['status']) && $result['status'] == 0) {
  32. $distance = isset($result['result']['elements'][0]['distance']) ? $result['result']['elements'][0]['distance'] : 0;
  33. }
  34. return $distance;
  35. }
  36. }