diff --git a/routes/map_api.py b/routes/map_api.py index eadffda..d15dd20 100644 --- a/routes/map_api.py +++ b/routes/map_api.py @@ -266,6 +266,44 @@ def api_config(): }) +@bp.route("/api/nav_route") +def api_nav_route(): + """导航路线:距离/耗时/分段指引/整条折线(前端直接画到地图上)。 + + 参数:origin_lat/origin_lng/dest_lat/dest_lng(GCJ02),mode=walking|riding + 返回:{rtn_code:0, data:{distance, duration, points:[[lng,lat],...], steps:[...]}} + """ + user = get_current_user() + if not user: + return jsonify({"rtn_code": 401, "message": "请先登录后使用"}), 401 + + from routes.auth import _client_ip + from rate_limit import hit as rl_hit + # 导航比查卡片更重,按 IP 每分钟 20 次 + if not rl_hit(("nav_route", _client_ip()), 20, 60): + return jsonify({"rtn_code": 429, "message": "导航请求过于频繁,请稍后再试"}), 429 + + try: + o_lat = float(request.args.get("origin_lat")) + o_lng = float(request.args.get("origin_lng")) + d_lat = float(request.args.get("dest_lat")) + d_lng = float(request.args.get("dest_lng")) + except (TypeError, ValueError): + return jsonify({"rtn_code": 400, "message": "缺少或非法的坐标参数"}) + + mode = (request.args.get("mode") or "walking").strip().lower() + if mode not in ("walking", "riding"): + mode = "walking" + + import walk_route + data = walk_route.get_nav_route(o_lat, o_lng, d_lat, d_lng, api=mode) + if data.get("error"): + return jsonify({"rtn_code": 500, "message": data["error"]}), 200 + response = jsonify({"rtn_code": 0, "data": data}) + response.headers["Cache-Control"] = "private, max-age=30" + return response + + @bp.route("/api/walk_route") def api_walk_route(): """步行路线:真实道路距离 + 步行耗时(百度轻量级路线规划)。 diff --git a/static/css/map.css b/static/css/map.css index d153a86..15bf5e9 100644 --- a/static/css/map.css +++ b/static/css/map.css @@ -2752,3 +2752,41 @@ border-color: var(--ui-line-strong) !important; } + +/* 导航信息条:浮在底部扫码栏上方 */ +.nav-banner { + position: fixed; + left: 12px; + right: 12px; + bottom: calc(84px + env(safe-area-inset-bottom, 0px)); + z-index: 260; + display: flex; + align-items: center; + justify-content: space-between; + gap: 10px; + padding: 10px 12px; + border-radius: 14px; + background: var(--bg-card, #fff); + border: 1px solid var(--border-color, #e5e5ea); + box-shadow: 0 6px 24px rgba(0, 0, 0, 0.18); +} +.nav-banner[hidden] { display: none; } +.nav-banner-main { display: flex; align-items: center; gap: 8px; min-width: 0; } +.nav-banner-icon { font-size: 16px; line-height: 1; } +.nav-banner-text { + font-size: 13px; + color: var(--text-primary, #1c1c1e); + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} +.nav-banner-stop { + flex-shrink: 0; + padding: 6px 12px; + border-radius: 10px; + border: none; + background: var(--bg-secondary, #f2f2f7); + color: var(--text-primary, #1c1c1e); + font-size: 12.5px; + font-weight: 600; +} diff --git a/static/js/map.js b/static/js/map.js index 3b4f710..6f9d237 100644 --- a/static/js/map.js +++ b/static/js/map.js @@ -1378,6 +1378,121 @@ } catch (e) { /* 保留直线估算 */ } } + // ========== 导航:画「我 -> 车」的路线 ========== + let _navOverlays = []; + let _navBikeCode = null; + let _navLoading = false; + + function _clearNavOverlays() { + _navOverlays.forEach(ov => { try { map.removeOverlay(ov); } catch (e) {} }); + _navOverlays = []; + _navBikeCode = null; + const banner = document.getElementById("nav-banner"); + if (banner) banner.hidden = true; + const btnText = document.getElementById("bike-nav-btn-text"); + if (btnText) btnText.textContent = "导航"; + } + + function stopNavigation() { + _clearNavOverlays(); + } + + function toggleNavigation() { + const bike = selectedBike; + if (!bike) return; + if (_navBikeCode && String(_navBikeCode) === String(bike.code)) { + stopNavigation(); // 再点一次 = 结束 + return; + } + startNavigation(bike); + } + + async function startNavigation(bike) { + if (_navLoading) return; + const me = (typeof autoLocationMarker !== "undefined" && autoLocationMarker) + ? autoLocationMarker.getPosition() : null; + if (!me) { + showToast("请先开启定位,才能规划路线"); + return; + } + if (!bike || !bike.latitude || !bike.longitude) { + showToast("这辆车没有坐标,无法导航"); + return; + } + + _clearNavOverlays(); + _navLoading = true; + const banner = document.getElementById("nav-banner"); + const bannerText = document.getElementById("nav-banner-text"); + if (banner) banner.hidden = false; + if (bannerText) bannerText.textContent = "正在规划路线…"; + + try { + const gcj = (typeof coordtransform !== "undefined" && coordtransform.bd09togcj02) + ? coordtransform.bd09togcj02(me.lng, me.lat) : null; + if (!gcj) throw new Error("坐标转换不可用"); + + const url = "/api/nav_route?mode=walking" + + "&origin_lat=" + gcj[1] + "&origin_lng=" + gcj[0] + + "&dest_lat=" + bike.latitude + "&dest_lng=" + bike.longitude; + const resp = await fetch(url, { headers: { "Authorization": "Bearer " + authToken } }); + const json = await resp.json(); + const d = json && json.data; + if (json.rtn_code !== 0 || !d || !d.points || !d.points.length) { + throw new Error((json && json.message) || "没有拿到路线"); + } + + // GCJ02 折线 -> BD09,画在地图上 + const bdPoints = d.points.map(pt => { + const c = offsetCoord(pt[1], pt[0]); // offsetCoord(lat, lng) -> BD09 + return new BMapGL.Point(c.lng, c.lat); + }); + // 白色描边 + 蓝色主线:与「骑行区域边界」(#007AFF 细线) 区分开 + const casing = new BMapGL.Polyline(bdPoints, { + strokeColor: "#FFFFFF", strokeWeight: 11, + strokeOpacity: 0.95, strokeStyle: "solid" + }); + const line = new BMapGL.Polyline(bdPoints, { + strokeColor: "#0A84FF", strokeWeight: 6, + strokeOpacity: 1, strokeStyle: "solid" + }); + map.addOverlay(casing); + map.addOverlay(line); + _navOverlays.push(casing, line); + + const endPoint = new BMapGL.Point(bdPoints[bdPoints.length - 1].lng, + bdPoints[bdPoints.length - 1].lat); + const endMarker = new BMapGL.Marker(endPoint); + map.addOverlay(endMarker); + _navOverlays.push(endMarker); + + if (typeof map.setViewport === "function") { + map.setViewport(bdPoints, { margins: [70, 70, 220, 70] }); + } + + _navBikeCode = bike.code; + const btnText = document.getElementById("bike-nav-btn-text"); + if (btnText) btnText.textContent = "结束"; + + const minutes = Math.max(1, Math.round(d.duration / 60)); + const distText = d.distance < 1000 + ? Math.round(d.distance) + " 米" + : (d.distance / 1000).toFixed(1) + " 公里"; + const first = (d.steps && d.steps[0] && d.steps[0].instruction) || ""; + const hint = first.replace(/<[^>]+>/g, "").slice(0, 14); + if (bannerText) { + bannerText.textContent = "步行 " + distText + " · 约 " + minutes + " 分钟" + + (hint ? " " + hint : ""); + } + } catch (err) { + console.error("[导航] 失败", err); + if (banner) banner.hidden = true; + showToast("导航失败:" + (err && err.message ? err.message : "请稍后再试")); + } finally { + _navLoading = false; + } + } + // 根据当前模式筛选车辆 function getFilteredBikes() { const allBikes = Object.values(markersData); @@ -3609,6 +3724,7 @@ } function closeCard() { + try { if (typeof stopNavigation === "function") stopNavigation(); } catch (e) {} const card = document.getElementById("bike-card"); const overlay = document.getElementById("card-overlay"); card.classList.remove("show"); diff --git a/templates/map.html b/templates/map.html index 0a76b07..5de88e6 100644 --- a/templates/map.html +++ b/templates/map.html @@ -193,6 +193,15 @@ + + +
@@ -720,6 +729,10 @@ #51287
+