| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
- use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
- use Darabonba\OpenApi\Models\Config;
- use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
- use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
- /**
- * 地图管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services
- */
- class MapService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- protected static $apiKey = '95b70f90da5e872f290ac97a4af5fa3c';
- protected $apiUrl = 'https://restapi.amap.com';
- protected $apiUrls = [
- 'driving'=> '/v5/direction/driving?%s'
- ];
- /**
- * 静态入口
- */
- public static function make(){
- if(!self::$instance){
- self::$instance = new static();
- }
- $mapKey = ConfigService::make()->getConfigByCode('gd_map_key');
- if($mapKey){
- self::$apiKey = $mapKey;
- }
- return self::$instance;
- }
- /**
- * 获取推荐规划线路的实际路程/m
- * @param $lat 起点 纬度
- * @param $lng 起点经度
- * @param $departLat 终点 纬度
- * @param $departLng 终点经度
- * @param int $strategy 驾车算路策略:32-地图APP默认,1-费用优先,2-距离优先,3-速度优先,34-高速优先,35-不走高速,38-速度最快
- * @return bool
- */
- public function getDrivingDistance($lat,$lng, $departLat, $departLng, $strategy=1)
- {
- // 参数
- $params = "key=".self::$apiKey."&origin={$lng},{$lat}&destination={$departLng},{$departLat}&strategy={$strategy}";
- $url = $this->apiUrl.sprintf($this->apiUrls['driving'], $params);
- $result = httpRequest($url,'','get','',5);
- $status = isset($result['status'])? intval($result['status']) : 0;
- $routes = isset($result['route'])? $result['route'] : [];
- $paths = isset($routes['paths'])? $routes['paths'] : [];
- $drivingLine = isset($paths[0])? $paths[0] : [];
- $distance = isset($drivingLine['distance'])? $drivingLine['distance'] : 0;
- RedisService::set('caches:map:distance:'.md5($params), ['url'=> $url,'params'=> $params,'line'=> $drivingLine,'result'=>$result], 7200);
- if($status == 1 && $distance>0){
- return $distance;
- }
- // 线路规划失败,按直线距离算
- else{
- return getDistance($lat,$lng,$departLat,$departLng);
- }
- }
- }
|