عرض طبقة حركة مرور حيّة
عرض طبقة حركة مرور حيّة
اعرض الازدحام في الوقت الحقيقي — المصدر من شبكة الاستشعار الخاصة بـ Navvo — كطبقة تراكب على خريطتك. النمط: احسب البلاطات التي تغطّي منطقة العرض، واجلب كلًّا منها، واعرض الخطوط الملوّنة. يستخدم حركة المرور.
النطاق المطلوب: traffic
الخيار أ — بلاطات GeoJSON (نسّقها بنفسك)
1. احصل على البلاطات التي تغطّي منطقة العرض
يُعيد GET /traffic/lines/tile-cover روابط البلاطات الدقيقة لإطار حدودي (bbox):
const bounds = map.getBounds(); // Navvo Maps
const bbox = [bounds.getWest(), bounds.getSouth(), bounds.getEast(), bounds.getNorth()].join(",");
const cover = await (await fetch(
"https://navvo.io/api/v1/traffic/lines/tile-cover?" + new URLSearchParams({ bbox, zoom: "14" }),
{ headers: { "x-api-key": KEY } }
)).json();
// cover.tiles -> [{ z, x, y, url }]
2. اجلب البلاطات وادمجها
const features = [];
for (const t of cover.tiles) {
const fc = await (await fetch(`https://navvo.io${t.url}`, { headers: { "x-api-key": KEY } })).json();
if (fc.hasData) features.push(...fc.features);
}
const geojson = { type: "FeatureCollection", features };
3. اعرض بألوان الازدحام
تحمل كل مَعلَم خاصية color مرتبطة بمستوى ازدحامه:
map.addSource("traffic", { type: "geojson", data: geojson });
map.addLayer({
id: "traffic",
type: "line",
source: "traffic",
paint: {
"line-color": ["get", "color"],
"line-width": ["interpolate", ["linear"], ["zoom"], 10, 1.5, 16, 5],
},
});
حدّث كل ~60 ثانية (قيمة refreshSeconds في واصف الطبقة).
الخيار ب — بلاطات متجهية (مصدر Navvo Maps الأصلي)
يقدّم تراكب القياسات أيضًا بلاطات متجهية ثنائية، والتي يمكن لـ Navvo Maps استهلاكها مباشرةً دون جلب يدوي:
map.addSource("traffic", {
type: "vector",
tiles: ["https://navvo.io/api/v1/telemetry/traffic-overlay/vector/{z}/{x}/{y}.pbf?mode=current"],
minzoom: 8,
maxzoom: 16,
});
map.addLayer({
id: "traffic",
type: "line",
source: "traffic",
"source-layer": "navvo_traffic",
paint: {
"line-width": 3,
"line-color": [
"match", ["get", "congestion"],
"light", "#188038",
"moderate", "#D9A441",
"heavy", "#D94B4B",
"blocked", "#8B1A1A",
"#188038",
],
},
});
حركة المرور النمطية (المتوقّعة)
للحصول على "كيف تبدو حركة المرور عادةً" بدلًا من اللحظة الحالية، استخدم بلاطات الخطوط المتوقّعة (GET /traffic/predicted/tiles/{z}/{x}/{y}) أو التراكب مع mode=typical.
شارة ملخّص
للحصول على قراءة ازدحام واحدة فوق منطقة، يُعيد GET /traffic/summary قيمة congestionScore وlevel المهيمن وbreakdown لكل مستوى:
const s = await (await fetch(
"https://navvo.io/api/v1/traffic/summary?bbox=" + bbox, { headers: { "x-api-key": KEY } }
)).json();
// s.conditions.summary -> "Slow moving"
hasData بقيمة false — ارجع إلى الطبقة المتوقّعة/النمطية للحصول على صورة أكمل.