admin 发表于 2026-3-18 18:07:23

调用腾讯地图计算两点间的距离

代码:

// 返回值
            $return = [
                'distance' => 0
            ];
            // 接口地址
            $url = "https://apis.map.qq.com/ws/direction/v1/driving/?from={$config['storelat']},{$config['storelng']}&to={$params['lat']},{$params['lng']}&output=json&callback=cb&key={$config['qqmap_apikey']}";
            $response = Http::get($url);
            $data = json_decode($response, true);
            // \think\Log::record($data['result']['routes']['distance']);
            if (isset($data['status']) && $data['status'] == 0) {
                // 导航距离
                $distance = $data['result']['routes']['distance'] / 1000;
                $return['distance'] = $distance;
                // 运费
                $delivery = SkuDelivery::where('status', 1)
                  ->where('distance_start', '<', $distance)
                  ->where('distance_end', '>=', $distance)
                  ->find();
                if (empty($delivery)) {
                  $this->error('抱歉,您的地址超出配送范围,请更换地址');
                }
                $return['deliveryfee'] = $delivery->payfee;
                $return['totalfee'] = bcadd($params['payfee'], $delivery->payfee, 2);
                $this->success('请求成功', $return);
            } else {
                $this->error('地图接口调用失败');
            }

页: [1]
查看完整版本: 调用腾讯地图计算两点间的距离