FastAPI + SQLAlchemy + asyncio TCP 服务器,支持设备管理、实时定位、 告警、考勤打卡、蓝牙记录、指令下发、TTS语音播报等功能。
56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
"""Geocoding proxy endpoints — keeps AMAP_KEY server-side."""
|
|
|
|
from fastapi import APIRouter, Query
|
|
import httpx
|
|
|
|
from app.config import settings
|
|
from app.geocoding import reverse_geocode, _amap_sign, AMAP_KEY
|
|
|
|
router = APIRouter(prefix="/api/geocode", tags=["geocoding"])
|
|
|
|
|
|
@router.get("/search")
|
|
async def search_location(
|
|
keyword: str = Query(..., min_length=1, max_length=100),
|
|
city: str = Query(default="", max_length=50),
|
|
):
|
|
"""Proxy for Amap POI text search. Returns GCJ-02 coordinates."""
|
|
if not AMAP_KEY:
|
|
return {"results": []}
|
|
params = {
|
|
"key": AMAP_KEY,
|
|
"keywords": keyword,
|
|
"output": "json",
|
|
"offset": "10",
|
|
"page": "1",
|
|
}
|
|
if city:
|
|
params["city"] = city
|
|
sig = _amap_sign(params)
|
|
if sig:
|
|
params["sig"] = sig
|
|
async with httpx.AsyncClient(timeout=5.0) as client:
|
|
resp = await client.get("https://restapi.amap.com/v3/place/text", params=params)
|
|
data = resp.json()
|
|
if data.get("status") != "1":
|
|
return {"results": []}
|
|
results = []
|
|
for poi in data.get("pois", [])[:10]:
|
|
if poi.get("location"):
|
|
results.append({
|
|
"name": poi.get("name", ""),
|
|
"address": poi.get("address", ""),
|
|
"location": poi["location"], # "lng,lat" in GCJ-02
|
|
})
|
|
return {"results": results}
|
|
|
|
|
|
@router.get("/reverse")
|
|
async def reverse_geocode_endpoint(
|
|
lat: float = Query(..., ge=-90, le=90),
|
|
lon: float = Query(..., ge=-180, le=180),
|
|
):
|
|
"""Reverse geocode WGS-84 coords to address via Amap."""
|
|
address = await reverse_geocode(lat, lon)
|
|
return {"address": address or ""}
|