如何在地图上显示实时路况 - 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; }