Test

mail@pastecode.io avatar
unknown
html
a month ago
1.8 kB
2
Indexable
Never
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>RoutePoTZ</title>
  <link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
  <script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
  <script src="https://unpkg.com/leaflet.markercluster/dist/leaflet.markercluster.js"></script>
  <link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster/dist/MarkerCluster.css" />
  <link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster/dist/MarkerCluster.Default.css" />

  <style>
    #map {
      height: 80vh;
    }
  </style>
</head>
<body>
  <div id="map"></div>

  <script>
    // Карта
    var map = L.map('map').setView([50.98192145950313, 6.072423309087753], 14);

    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);

    // Данные с сервера
    fetch('http://localhost:3000/car-track-json')
      .then(response => response.json())
      .then(data => {
        var coordinates = data.map(point => [point.latitude, point.longitude]);
    var markers = L.markerClusterGroup();

    // Добавление маркеров в группу
    coordinates.forEach(coord => {
      var marker = L.marker(coord);
      markers.addLayer(marker);
    });

    map.addLayer(markers);
            coordinates.forEach(coord => {
              L.marker(coord).addTo(map);
            });

            // Строим маршрут
            L.polyline(coordinates, {color: 'red'}).addTo(map);
          })
          .catch(error => console.error('Ошибка получения данных:', error));
  </script>
</body>
</html>
Leave a Comment