如何在地圖上顯示即時流量 - Amazon Location Service

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

如何在地圖上顯示即時流量

使用 Amazon Location Service,您可以將流量功能新增至地圖。這可提供即時流量資料,以顯示目前的流量條件,例如目前的道路擁塞、建構和事件,協助您進行路由選擇。

建立具有即時流量的映射

此範例示範如何使用即時流量資料建立映射。

index.html
<!DOCTYPE html> <html lang="en"> <head> <title>Traffic Map</title> <meta charset='utf-8'> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel='stylesheet' href='https://unpkg.com/maplibre-gl@5.x/dist/maplibre-gl.css' /> <script src='https://unpkg.com/maplibre-gl@5.x/dist/maplibre-gl.js'></script> </head> <body style="margin: 0; padding: 0;"> <div id="map" style="width: 100%; height: 100vh;"></div> <script> const apiKey = "Add Your Api Key"; const mapStyle = "Standard"; const awsRegion = "us-east-1"; const map = new maplibregl.Map({ container: 'map', style: `https://maps.geo.${awsRegion}.amazonaws.com/v2/styles/${mapStyle}/descriptor?traffic=All&key=${apiKey}`, center: [-74.0060 ,40.7128], // New York City coordinates for traffic visibility zoom: 12, validateStyle: false, // Disable style validation for faster map load }); </script> </body> </html>
style.css
body { margin: 0; padding: 0; } #map { width: 100%; height: 100vh; }