map.component.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { Component, OnInit, Inject, PLATFORM_ID, AfterViewInit } from '@angular/core';
  2. import { isPlatformBrowser } from '@angular/common';
  3. import { Restaurant } from './../../../../models/restaurant';
  4. import { RestaurantsService } from 'src/app/services/restaurants.service';
  5. import { LeafletService } from 'src/app/services/leaflet.service';
  6. @Component({
  7. selector: 'app-map',
  8. templateUrl: './map.component.html',
  9. styleUrls: ['./map.component.scss']
  10. })
  11. export class MapComponent implements OnInit, AfterViewInit {
  12. private L: any;
  13. private macarte: any;
  14. constructor(
  15. private restaurantsService: RestaurantsService,
  16. private leafletService: LeafletService,
  17. @Inject(PLATFORM_ID) private platformId: Object
  18. ) {}
  19. ngOnInit() {
  20. if (isPlatformBrowser(this.platformId)) {
  21. this.L = this.leafletService.getLeaflet();
  22. }
  23. }
  24. ngAfterViewInit() {
  25. if (isPlatformBrowser(this.platformId)) {
  26. this.initMap();
  27. // Corriger le découpage de la carte après affichage
  28. setTimeout(() => {
  29. this.macarte.invalidateSize();
  30. }, 500);
  31. }
  32. }
  33. private initMap(): void {
  34. if (!this.L) return;
  35. this.macarte = this.L.map('frugalmap').setView([48.85513, 2.353429], 9);
  36. // Utiliser HTTPS pour éviter les erreurs Mixed Content
  37. this.L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {
  38. attribution: 'FatBoar',
  39. }).addTo(this.macarte);
  40. const myIcon = this.L.icon({
  41. iconUrl: 'assets/img/marker/markericon.png',
  42. iconSize: [38, 38],
  43. iconAnchor: [19, 38],
  44. });
  45. this.restaurantsService.getRestaurants().subscribe((data: any) => {
  46. data.forEach((res: Restaurant) => {
  47. const marker = this.L.marker([res.latitude, res.longitude], { icon: myIcon }).addTo(this.macarte);
  48. marker.bindPopup(
  49. `<h5 style="text-align: center"><b>${res.name}</b></h5><b>Adresse :</b> ${res.adress}`,
  50. { maxWidth: 130, minWidth: 120 }
  51. );
  52. });
  53. });
  54. }
  55. }