Обломки карты: Google Maps API, Leaflet.js и Mapbox API

Фраза «составить карту мусора freecodecamp» представляет собой комбинацию ключевых слов, связанных с картографированием и мусором. Если вы ищете методы для картирования мусора, вот несколько подходов и примеры кода:

  1. API Карт Google:

    • Описание: используйте API Google Maps для отображения местоположений обломков на карте.
    • Пример кода:
      // HTML
      <div id="map"></div>
      // JavaScript
      function initMap() {
      const map = new google.maps.Map(document.getElementById('map'), {
       center: { lat: 37.7749, lng: -122.4194 }, // Set the initial map center
       zoom: 10, // Set the initial zoom level
      });
      // Add markers for debris locations
      const debrisLocations = [
       { lat: 37.1234, lng: -122.5678 },
       { lat: 37.9876, lng: -122.3456 },
       // Add more locations as needed
      ];
      debrisLocations.forEach((location) => {
       new google.maps.Marker({
         position: location,
         map,
       });
      });
      }
  2. Leaflet.js:

    • Описание: используйте библиотеку Leaflet.js для создания интерактивной карты с маркерами обломков.
    • Пример кода:
      // HTML
      <div id="map"></div>
      // JavaScript
      const map = L.map('map').setView([37.7749, -122.4194], 10); // Set the initial map center and zoom level
      L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
      attribution: 'Map data © OpenStreetMap contributors',
      }).addTo(map);
      // Add markers for debris locations
      const debrisLocations = [
      { lat: 37.1234, lng: -122.5678 },
      { lat: 37.9876, lng: -122.3456 },
      // Add more locations as needed
      ];
      debrisLocations.forEach((location) => {
      L.marker([location.lat, location.lng]).addTo(map);
      });
  3. API Mapbox:

    • Описание: используйте API Mapbox для создания карты и отображения маркеров мусора.
    • Пример кода:
      // HTML
      <div id="map"></div>
      <script src="https://api.mapbox.com/mapbox-gl-js/v2.3.1/mapbox-gl.js"></script>
      <link href="https://api.mapbox.com/mapbox-gl-js/v2.3.1/mapbox-gl.css" rel="stylesheet" />
      // JavaScript
      mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
      const map = new mapboxgl.Map({
      container: 'map',
      style: 'mapbox://styles/mapbox/streets-v11', // Specify the map style
      center: [-122.4194, 37.7749], // Set the initial map center
      zoom: 10, // Set the initial zoom level
      });
      // Add markers for debris locations
      const debrisLocations = [
      { lat: 37.1234, lng: -122.5678 },
      { lat: 37.9876, lng: -122.3456 },
      // Add more locations as needed
      ];
      debrisLocations.forEach((location) => {
      new mapboxgl.Marker().setLngLat([location.lng, location.lat]).addTo(map);
      });