feat: 地图导航——点「导航」画出到车的步行路线
## 后端 - walk_route.get_nav_route():一次拿齐 distance/duration/steps/points - steps[] 抽成 instruction(含高亮标签)、turn_type、distance、duration、path - 分段折线合并成整条,超过 1500 点按等间隔抽稀 - SN 签名与接口路径解耦(build_query 支持传 path),riding/driving 也可用 - 新增 /api/nav_route(需登录,按 IP 20 次/分钟限流),mode=walking|riding ## 前端 - 车辆卡片新增「导航」按钮:拉 /api/nav_route → GCJ02 折线转 BD09 → 白色描边 + 蓝色主线画在地图上(与 #007AFF 的骑行边界区分) → 加终点标记 → setViewport 自动适配视野 - 底部导航信息条:「步行 424 米 · 约 6 分钟 向东南方向出发…」,带「结束导航」 - 再点一次按钮 / 关闭卡片都会清掉路线 实测:校内站位与车直线相距约 310 米,百度步行 424 米 / 6 分钟,折线与路网贴合。 136 pytest + 29 node 全绿。
This commit is contained in:
+79
-3
@@ -237,7 +237,7 @@ def estimate(origin_lat, origin_lng, dest_lat, dest_lng) -> dict:
|
||||
# ========== 百度接口 ==========
|
||||
|
||||
def _call_baidu(ak, sk, origin_lat, origin_lng, dest_lat, dest_lng,
|
||||
mode="official") -> dict:
|
||||
mode="official", api="walking", with_steps=False) -> dict:
|
||||
_throttle()
|
||||
params = {
|
||||
"ak": ak,
|
||||
@@ -248,7 +248,8 @@ def _call_baidu(ak, sk, origin_lat, origin_lng, dest_lat, dest_lng,
|
||||
if sk:
|
||||
# 开了 SN 校验就必须带 timestamp(否则 status=2 timestamp is required when sn isset)
|
||||
params["timestamp"] = int(time.time())
|
||||
url = BASE_URL + "?" + build_query(params, sk, mode)
|
||||
api_path = "/directionlite/v1/" + api
|
||||
url = HOST + api_path + "?" + build_query(params, sk, mode, api_path)
|
||||
resp = requests.get(url, timeout=REQUEST_TIMEOUT)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
@@ -263,7 +264,82 @@ def _call_baidu(ak, sk, origin_lat, origin_lng, dest_lat, dest_lng,
|
||||
duration = route.get("duration", result.get("duration"))
|
||||
if distance is None or duration is None:
|
||||
raise RuntimeError(f"返回缺字段: {str(data)[:200]}")
|
||||
return {"distance": int(distance), "duration": int(duration)}
|
||||
out = {"distance": int(distance), "duration": int(duration)}
|
||||
if with_steps:
|
||||
out["steps"] = _parse_steps(route.get("steps") or [])
|
||||
out["points"] = _merge_path(out["steps"])
|
||||
return out
|
||||
|
||||
|
||||
def _parse_steps(raw_steps) -> list:
|
||||
"""把 steps[] 抽成前端要的最小集合(含折线)。"""
|
||||
steps = []
|
||||
for st in raw_steps:
|
||||
if not isinstance(st, dict):
|
||||
continue
|
||||
points = []
|
||||
for pair in str(st.get("path") or "").split(";"):
|
||||
if not pair:
|
||||
continue
|
||||
try:
|
||||
lng_s, lat_s = pair.split(",")
|
||||
points.append([float(lng_s), float(lat_s)])
|
||||
except ValueError:
|
||||
continue
|
||||
steps.append({
|
||||
"instruction": str(st.get("instruction") or ""),
|
||||
"distance": int(st.get("distance") or 0),
|
||||
"duration": int(st.get("duration") or 0),
|
||||
"turn_type": str(st.get("turn_type") or ""),
|
||||
"turn_type_id": st.get("turn_type_id"),
|
||||
"points": points,
|
||||
})
|
||||
return steps
|
||||
|
||||
|
||||
def _merge_path(steps, max_points=1500) -> list:
|
||||
"""把所有分段的折线接成一条,超长时按等间隔抽稀。"""
|
||||
points = []
|
||||
for st in steps:
|
||||
pts = st.get("points") or []
|
||||
if points and pts and points[-1] == pts[0]:
|
||||
pts = pts[1:]
|
||||
points.extend(pts)
|
||||
if len(points) > max_points:
|
||||
stride = max(1, len(points) // max_points)
|
||||
sampled = points[::stride]
|
||||
if sampled[-1] != points[-1]:
|
||||
sampled.append(points[-1])
|
||||
return sampled
|
||||
return points
|
||||
|
||||
|
||||
def get_nav_route(origin_lat, origin_lng, dest_lat, dest_lng, api="walking") -> dict:
|
||||
"""导航用:一次拿齐距离/耗时/分段指引/整条折线。失败返回 error。"""
|
||||
try:
|
||||
o_lat, o_lng = float(origin_lat), float(origin_lng)
|
||||
d_lat, d_lng = float(dest_lat), float(dest_lng)
|
||||
except (TypeError, ValueError):
|
||||
return {"error": "坐标非法"}
|
||||
|
||||
ak = get_ak()
|
||||
if not ak:
|
||||
return {"error": "未配置百度 AK"}
|
||||
|
||||
sk = get_sk()
|
||||
last_error = None
|
||||
for mode in _sn_modes():
|
||||
try:
|
||||
got = _call_baidu(ak, sk, o_lat, o_lng, d_lat, d_lng, mode,
|
||||
api=api, with_steps=True)
|
||||
_SN_MODE[0] = mode
|
||||
return {**got, "api": api}
|
||||
except Exception as exc:
|
||||
last_error = exc
|
||||
text = str(exc)
|
||||
if "211" not in text and "SN" not in text.upper():
|
||||
break
|
||||
return {"error": f"百度返回失败: {last_error}"}
|
||||
|
||||
|
||||
def get_walk_route(origin_lat, origin_lng, dest_lat, dest_lng, use_cache=True) -> dict:
|
||||
|
||||
Reference in New Issue
Block a user