You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.5 KiB
77 lines
2.5 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class MapController extends Controller
|
|
{
|
|
public function search(Request $request): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'keyword' => ['required', 'string', 'max:120'],
|
|
'region' => ['nullable', 'string', 'max:80'],
|
|
]);
|
|
|
|
$key = env('TENCENT_MAP_SERVER_KEY');
|
|
abort_unless($key, 500, '未配置腾讯地图服务端Key');
|
|
|
|
$resp = Http::get('https://apis.map.qq.com/ws/place/v1/suggestion', [
|
|
'keyword' => $data['keyword'],
|
|
'region' => $data['region'] ?? '苏州',
|
|
'region_fix' => 1,
|
|
'key' => $key,
|
|
])->json();
|
|
|
|
if (($resp['status'] ?? -1) !== 0) {
|
|
return response()->json(['message' => $resp['message'] ?? '地图搜索失败'], 422);
|
|
}
|
|
|
|
$rows = collect($resp['data'] ?? [])->map(function ($item) {
|
|
return [
|
|
'title' => $item['title'] ?? '',
|
|
'address' => $item['address'] ?? '',
|
|
'province' => $item['province'] ?? '',
|
|
'city' => $item['city'] ?? '',
|
|
'district' => $item['district'] ?? '',
|
|
'lat' => data_get($item, 'location.lat'),
|
|
'lng' => data_get($item, 'location.lng'),
|
|
];
|
|
})->values();
|
|
|
|
return response()->json($rows);
|
|
}
|
|
|
|
public function reverseGeocode(Request $request): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'lat' => ['required', 'numeric'],
|
|
'lng' => ['required', 'numeric'],
|
|
]);
|
|
|
|
$key = env('TENCENT_MAP_SERVER_KEY');
|
|
abort_unless($key, 500, '未配置腾讯地图服务端Key');
|
|
|
|
$resp = Http::get('https://apis.map.qq.com/ws/geocoder/v1/', [
|
|
'location' => $data['lat'] . ',' . $data['lng'],
|
|
'get_poi' => 0,
|
|
'key' => $key,
|
|
])->json();
|
|
|
|
if (($resp['status'] ?? -1) !== 0) {
|
|
return response()->json(['message' => $resp['message'] ?? '逆地理编码失败'], 422);
|
|
}
|
|
|
|
$result = $resp['result'] ?? [];
|
|
return response()->json([
|
|
'address' => $result['address'] ?? '',
|
|
'province' => data_get($result, 'address_component.province'),
|
|
'city' => data_get($result, 'address_component.city'),
|
|
'district' => data_get($result, 'address_component.district'),
|
|
]);
|
|
}
|
|
}
|