diff --git a/js/main.js b/js/main.js index eec78f6..740144e 100644 --- a/js/main.js +++ b/js/main.js @@ -1,13 +1,103 @@ // /Volumes/Data/Code/FHGR/Frontend/js/main.js document.addEventListener('DOMContentLoaded', function() { - // Initialize global variables + // Initialize current journey when page loads window.currentJourney = { id: Date.now(), - name: "Untitled Journey", + name: "", description: "", markers: [], path: null }; + + // Function to prepare and save the journey + function prepareAndSaveJourney() { + const journeyData = { + name: document.getElementById('journey-title').value, + description: document.getElementById('journey-description').value, + markers: window.currentJourney.markers.map(marker => ({ + id: marker.id, + lngLat: [marker.getLatLng().lat, marker.getLatLng().lng], + content: marker.content + })) + }; + + // Save to backend + fetch('http://localhost:5000/api/journeys', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(journeyData) + }) + .then(response => response.json()) + .then(data => { + alert('Journey saved successfully!'); + window.currentJourney = { + id: Date.now(), + name: "", + description: "", + markers: [], + path: null + }; + document.getElementById('journey-title').value = ''; + document.getElementById('journey-description').value = ''; + }) + .catch(error => { + console.error('Error:', error); + alert('Failed to save journey. Please try again.'); + }); + } + + // Event listeners for the buttons + document.getElementById('add-marker-btn').addEventListener('click', function() { + map.on('click', function(e) { + const marker = L.marker(e.latlng, {draggable: true}).addTo(map); + + // Add popup with input field + marker.bindPopup(''); + + window.currentJourney.markers.push(marker); + updateMarkersList(); + }); + }); + + document.getElementById('save-journey-btn').addEventListener('click', prepareAndSaveJourney); + + document.getElementById('clear-markers-btn').addEventListener('click', function() { + map.eachLayer(function(layer) { + if (layer instanceof L.Marker) { + map.removeLayer(layer); + } + }); + window.currentJourney.markers = []; + updateMarkersList(); + }); + + function updateMarkersList() { + const markersContainer = document.getElementById('markers-container'); + markersContainer.innerHTML = ''; + + if (window.currentJourney.markers.length === 0) { + markersContainer.innerHTML = '

No markers yet. Click on the map to add markers.

'; + return; + } + + window.currentJourney.markers.forEach((marker, index) => { + const markerElement = document.createElement('div'); + markerElement.className = 'marker-item'; + markerElement.innerHTML = ` + ${index + 1} + ${marker.getLatLng().lat.toFixed(4)}, ${marker.getLngLat().lng.toFixed(4)} + `; + + // Add click event to edit marker + markerElement.addEventListener('click', () => { + marker.openPopup(); + }); + + markersContainer.appendChild(markerElement); + }); + } window.journeys = []; window.isCreatingJourney = true; diff --git a/map-page.html b/map-page.html index 7ea385e..8102a91 100644 --- a/map-page.html +++ b/map-page.html @@ -507,35 +507,34 @@ - +

Create New Journey

- +
- +
- +
- +
- -
-

How to create:

-
    -
  1. Click on the map to place markers
  2. -
  3. Click on a marker to add content
  4. -
  5. Markers will be automatically connected
  6. -
  7. Click "Save Journey" when done
  8. -
+ +

Journey Markers

+
+ +

No markers yet. Click on the map to add markers.

- +
- + -