MapService.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
  13. use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
  14. use Darabonba\OpenApi\Models\Config;
  15. use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
  16. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  17. /**
  18. * 地图管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * @package App\Services
  22. */
  23. class MapService extends BaseService
  24. {
  25. // 静态对象
  26. protected static $instance = null;
  27. protected static $apiKey = '95b70f90da5e872f290ac97a4af5fa3c';
  28. protected $apiUrl = 'https://restapi.amap.com';
  29. protected $apiUrls = [
  30. 'driving'=> '/v5/direction/driving?%s'
  31. ];
  32. /**
  33. * 静态入口
  34. */
  35. public static function make(){
  36. if(!self::$instance){
  37. self::$instance = new static();
  38. }
  39. $mapKey = ConfigService::make()->getConfigByCode('gd_map_key');
  40. if($mapKey){
  41. self::$apiKey = $mapKey;
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * 获取推荐规划线路的实际路程/m
  47. * @param $lat 起点 纬度
  48. * @param $lng 起点经度
  49. * @param $departLat 终点 纬度
  50. * @param $departLng 终点经度
  51. * @param int $strategy 驾车算路策略:32-地图APP默认,1-费用优先,2-距离优先,3-速度优先,34-高速优先,35-不走高速,38-速度最快
  52. * @return bool
  53. */
  54. public function getDrivingDistance($lat,$lng, $departLat, $departLng, $strategy=1)
  55. {
  56. // 参数
  57. $params = "key=".self::$apiKey."&origin={$lng},{$lat}&destination={$departLng},{$departLat}&strategy={$strategy}";
  58. $url = $this->apiUrl.sprintf($this->apiUrls['driving'], $params);
  59. $result = httpRequest($url,'','get','',5);
  60. $status = isset($result['status'])? intval($result['status']) : 0;
  61. $routes = isset($result['route'])? $result['route'] : [];
  62. $paths = isset($routes['paths'])? $routes['paths'] : [];
  63. $drivingLine = isset($paths[0])? $paths[0] : [];
  64. $distance = isset($drivingLine['distance'])? $drivingLine['distance'] : 0;
  65. RedisService::set('caches:map:distance:'.md5($params), ['url'=> $url,'params'=> $params,'line'=> $drivingLine,'result'=>$result], 7200);
  66. if($status == 1 && $distance>0){
  67. return $distance;
  68. }
  69. // 线路规划失败,按直线距离算
  70. else{
  71. return getDistance($lat,$lng,$departLat,$departLng);
  72. }
  73. }
  74. }