fix: 远距不导航 + 卡片按钮自适应 + 寻车换铃铛图标
- 直线距离 ≥ 20km 时卡片不再显示「导航」按钮(距离格同隐藏), 正在导航该车时仍保留「结束」 - startNavigation 兜底静默返回;_requestNavRoute 识别 too_far 不弹报错 - 后端 get_nav_route 调百度前按上限拦截(步行/骑行 200km、驾车 500km), 返回 error_kind=too_far 与中文提示;百度的 "the distance of path must less then 200Km" 也翻成中文 - 顺手修 get_nav_route 里 float(d_lng) -> float(dest_lng) 的笔误 (会让整个函数抛 UnboundLocalError) - 卡片顶部按钮是否收成纯图标改为实测:车牌被 ellipsis 截断才收, 同时考虑车牌长度与屏宽;resize 与「导航/结束」换字后重新测量 - 「寻车」放大镜换成描边铃铛 SVG,title 改为「寻车(响铃)」 测试:136 pytest + 30 node 全绿
This commit is contained in:
@@ -216,6 +216,21 @@ def _throttle():
|
||||
|
||||
# ========== 估算兜底 ==========
|
||||
|
||||
# 百度各出行方式的距离上限(公里)。步行超 200km 会直接报
|
||||
# "the distance of path must less then 200Km",先自己挡掉。
|
||||
_MAX_ROUTE_KM = {"walking": 200, "riding": 200, "driving": 500}
|
||||
|
||||
|
||||
def _haversine_m(lat1, lng1, lat2, lng2) -> float:
|
||||
"""两点球面距离(米)。"""
|
||||
r = 6371000.0
|
||||
p1, p2 = math.radians(lat1), math.radians(lat2)
|
||||
dphi = p2 - p1
|
||||
dlmb = math.radians(lng2 - lng1)
|
||||
a = math.sin(dphi / 2) ** 2 + math.cos(p1) * math.cos(p2) * math.sin(dlmb / 2) ** 2
|
||||
return 2 * r * math.asin(math.sqrt(a))
|
||||
|
||||
|
||||
def estimate(origin_lat, origin_lng, dest_lat, dest_lng) -> dict:
|
||||
"""直线 → 道路经验估算。永远有返回值。"""
|
||||
try:
|
||||
@@ -325,6 +340,15 @@ def get_nav_route(origin_lat, origin_lng, dest_lat, dest_lng, api="walking") ->
|
||||
except (TypeError, ValueError):
|
||||
return {"error": "坐标非法"}
|
||||
|
||||
# 百度步行/骑行路线对距离有上限(超过会返回 status=2 的英文报错),
|
||||
# 先自己算直线距离挡掉,别把 "the distance of path must less then 200Km" 抛给用户。
|
||||
max_km = _MAX_ROUTE_KM.get(api, 200)
|
||||
if max_km:
|
||||
straight_km = _haversine_m(o_lat, o_lng, d_lat, d_lng) / 1000.0
|
||||
if straight_km > max_km:
|
||||
return {"error": f"距离太远(约 {straight_km:.0f} 公里),不提供{api}导航",
|
||||
"error_kind": "too_far"}
|
||||
|
||||
ak = get_ak()
|
||||
if not ak:
|
||||
return {"error": "未配置百度 AK"}
|
||||
@@ -342,6 +366,9 @@ def get_nav_route(origin_lat, origin_lng, dest_lat, dest_lng, api="walking") ->
|
||||
text = str(exc)
|
||||
if "211" not in text and "SN" not in text.upper():
|
||||
break
|
||||
msg = str(last_error)
|
||||
if "200Km" in msg or "too long" in msg.lower():
|
||||
return {"error": "距离太远,无法规划路线", "error_kind": "too_far"}
|
||||
return {"error": f"百度返回失败: {last_error}"}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user