// Map initialization and marker management
function initMap() {
AppState.map = L.map('map').setView([51.1657, 10.4515], 6);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors',
maxZoom: 19,
}).addTo(AppState.map);
// Try to get user's location
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position) => {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
// Add user location marker
const userIcon = L.divIcon({
html: '
',
className: '',
iconSize: [16, 16],
iconAnchor: [8, 8]
});
L.marker([lat, lon], { icon: userIcon })
.addTo(AppState.map)
.bindPopup('Dein Standort
');
console.log('User location:', lat, lon);
},
(error) => {
console.log('Geolocation error:', error.message);
}
);
}
}
function clearMarkers() {
AppState.markers.forEach(marker => AppState.map.removeLayer(marker));
AppState.markers = [];
}
function addMarker(listing) {
if (!listing.lat || !listing.lon) return;
const marker = L.marker([listing.lat, listing.lon]).addTo(AppState.map);
const imageHtml = listing.image
? `
`
: '';
const popupContent = `
${listing.title}
${imageHtml}
${listing.price} €
${listing.address}
Link öffnen →
`;
marker.bindPopup(popupContent);
marker.on('click', () => {
AppState.selectedListingId = listing.id;
highlightSelectedListing();
});
AppState.markers.push(marker);
}