萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> Android 開發

基於 HTML5 + Qunee 構建互動式上海地鐵圖:數據建模、渲染與拖拽聯動實戰

項目背景

市政項目需展示上海地鐵站點、換乘、線路走向及實時信息流。選用 Qunee for HTML5(輕量 Canvas 圖形組件),快速實現可縮放、可拖拽、站點聯動的線上地鐵圖。

數據結構設計(JSON)

三大實體類型:

{
  "labels": [ { "text": "莘莊", "x": 883.591, "y": 1625.695, "rotate": 0.78 } ],
  "stations": [ { "id": 5, "x": 869.85, "y": 1597.66, "rotate": 0.785 } ],
  "lines": [
    { "name": "1", "color": "#e52035", "stations": [64, 70, 67, ..., { "id": 21, "yOffset": 0.5 }] },
    { "name": "3", "color": "#f9d300", "stations": [6, 95, ..., { "id": 12, "yOffset": 0.5 }] }
  ]
}
  • labels:站名標籤,含坐標、可選旋轉角度(避免重疊)。
  • stations:站點節點,含 id、坐標、旋轉(換乘站常用菱形+旋轉)。
  • lines:線路,含名稱、顏色、站點 id 序列;yOffset 處理共線偏移(如 3/4 號線共軌)。

核心渲染邏輯(Qunee API)

創建文本標籤

function createText(name, x, y, rotate) {
  const text = graph.createNode(name, x, y);
  if (rotate) text.rotate = rotate;
  text.zIndex = 20;
  text.image = null;
  text.setStyle(Q.Styles.BACKGROUND_COLOR, Q.toColor(0x88FFFFFF));
  text.setStyle(Q.Styles.LABEL_ANCHOR_POSITION, Q.Position.LEFT_BOTTOM);
  text.setStyle(Q.Styles.LABEL_POSITION, Q.Position.CENTER_MIDDLE);
  return text;
}

創建站點節點

function createStation(station) {
  const node = graph.createNode(null, station.x, station.y);
  node.stationId = station.id;
  node.setStyle(Q.Styles.LABEL_FONT_SIZE, 10);
  node.setStyle(Q.Styles.LABEL_ANCHOR_POSITION, Q.Position.CENTER_MIDDLE);
  node.zIndex = 10;
  if (station.rotate) {
    node.image = roundRect; node.rotate = station.rotate;
  } else { node.image = circle; }
  node.setStyle(Q.Styles.SHAPE_FILL_COLOR, "#FFF");
  node.setStyle(Q.Styles.SHAPE_STROKE_STYLE, "#000");
  return node;
}

創建線路(路徑節點)

function createLine(line) {
  const node = graph.createNode(line.name);
  node.stations = line.stations;
  node.movable = false;
  node.setStyle(Q.Styles.LABEL_FONT_SIZE, 50);
  node.setStyle(Q.Styles.LABEL_COLOR, line.color);
  node.setStyle(Q.Styles.LAYOUT_BY_PATH, true);
  node.setStyle(Q.Styles.SHAPE_STROKE, size);
  node.setStyle(Q.Styles.SHAPE_STROKE_STYLE, line.color);
  updateLine(node, true);
  return node;
}

function updateLine(line, addListener) {
  const path = new Q.Path();
  line.image = path;
  let first = true;
  Q.forEach(line.stations, s => {
    const station = getStation(s.id || s);
    if (!station) return;
    if (addListener) addLocationChangeListener(station.stationId, line);
    let {x, y} = station.location;
    if (s.yOffset) {
      const offset = s.yOffset * size;
      const rotate = station.rotate || 0;
      x += Math.cos(rotate) * offset;
      y += Math.sin(rotate) * offset;
    }
    first ? path.moveTo(x, y) : path.lineTo(x, y);
    first = false;
  });
}

交互:站點拖拽帶動線路重繪

graph.interactionDispatcher.addListener(evt => {
  if (evt.kind !== Q.InteractionEvent.ELEMENT_MOVING) return;
  Q.forEach(evt.datas, data => {
    if (!data.stationId) return;
    const listeners = stationLocationChangeListeners[data.stationId];
    if (listeners) for (const l in listeners) updateLine(listeners[l]);
  });
});

關鍵技術點總結

  1. 數據驅動視圖:JSON 分離數據與渲染,易維護、易擴展。
  2. 共線偏移算法:利用站點旋轉角計算正交偏移,完美解決多線重疊。
  3. 事件總線解耦:監聽節點移動,通過映射表精準更新相關線路,性能優異。
  4. Canvas 高性能:Qunee 底層 Canvas 渲染,萬級節點仍流暢。

遷移建議

若不依賴 Qunee,可用 D3.js + SVG 或 Canvas 原生 API 實現同樣邏輯:數據結構不變,渲染層替換即可。核心難點在路徑幾何計算與拖拽聯動機制,而非具體庫。

copyright © 萬盛學電腦網 all rights reserved