import { Component, OnInit, ViewContainerRef } from '@angular/core'; import * as L from 'leaflet'; import 'leaflet/dist/leaflet.css'; import { Monument } from '../../model/Monument'; import { PinService } from '../../services/pin.service'; import { MonumentMarkerComponent } from '../monument-marker/monument-marker.component'; @Component({ selector: 'app-leaflet-map', templateUrl: './leaflet-map.component.html', }) export class LeafletMapComponent implements OnInit { private map!: L.Map; constructor( private viewContainerRef: ViewContainerRef, private pinsService: PinService ) {} ngOnInit(): void { this.initializeMap(); } private initializeMap(): void { // Initialize the map this.map = L.map('map', { maxBounds: L.latLngBounds( L.latLng(-90, -180), // South-West L.latLng(90, 180) // North-East ), maxBoundsViscosity: 1.0, // Prevent dragging the map out of bounds minZoom: 2, // Prevent zooming out too much }).setView([46.603354, 1.888334], 6); // Add OpenStreetMap tiles L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '', }).addTo(this.map); this.map.attributionControl.setPrefix(''); // Define custom icons const visitedIcon = this.createDivIcon(` `); const notVisitedIcon = this.createDivIcon(` `); this.pinsService.getPins().subscribe((monuments: Monument[]) => { console.log(monuments); // Add markers monuments.forEach((monument: Monument) => { //const icon = monument.visited ? visitedIcon : notVisitedIcon; const icon = visitedIcon; const marker = L.marker(monument.location as [number, number], { icon, }).addTo(this.map); // Dynamically create Angular component and attach it to popup const popupDiv = document.createElement('div'); const componentRef = this.viewContainerRef.createComponent( MonumentMarkerComponent ); componentRef.instance.monument = monument; popupDiv.appendChild(componentRef.location.nativeElement); marker.bindPopup(popupDiv); }); }); } private createDivIcon(htmlContent: string): L.DivIcon { return L.divIcon({ html: htmlContent, className: 'text-2xl text-blue-500', iconSize: [24, 24], iconAnchor: [12, 24], popupAnchor: [0, -24], }); } }