This commit is contained in:
xman 2025-04-25 17:11:34 +08:00
parent 2b8cb7150b
commit bf418b8e3a
2 changed files with 60 additions and 0 deletions

View File

@ -194,4 +194,51 @@ if (!function_exists('build_heading')) {
}
return $result;
}
function getLocation($address){
try {
// 百度地图 API 的 URL 和 API Key
$apiKey = config('map.baidu_app_key'); // 替换为您的 API Key
// 构建请求 URL
$url = "http://api.map.baidu.com/geocoding/v3/?address=" . urlencode($address) . "&output=json&ak=" . $apiKey;
// 使用 cURL 请求接口
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
// 解析返回的 JSON 数据
$data = json_decode($response, true);
// 获取经纬度
if ($data['status'] == 0) {
$latitude = $data['result']['location']['lat']; // 纬度
$longitude = $data['result']['location']['lng']; // 经度
return [
'lat' => $latitude,
'lng' => $longitude,
];
} else {
return [];
//echo "地址解析失败: " . $data['msg'] . "\n";
}
}catch (Exception $exception){
return [];
}
}
}

View File

@ -2,6 +2,7 @@
namespace app\admin\controller\workers;
use app\admin\model\Area;
use app\admin\model\AuthGroup;
use app\admin\model\Item;
use app\admin\model\Order;
@ -158,6 +159,18 @@ class Worker extends Backend
$this->model->validateFailException()->validate($validate);
}
$params['admin_id'] = $this->auth->id;
if(!empty($params['area_id'])){
$area = Area::getByCode($params['area_id']);
if($area){
$location = getLocation($area->merge_name);
if(!empty($location)){
$params['lng'] = $location['lng'];
$params['lat'] = $location['lat'];
}
}
}
$result = $this->model->allowField(true)->save($params);
$item_map = model('item')->getAll();
$item_map = array_column($item_map, 'level', 'id');