All files / app/elements/g-map g-map.component.ts

87.34% Statements 145/166
73.68% Branches 14/19
76.92% Functions 10/13
87.34% Lines 145/166

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 1671x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 35x 35x 35x 35x 1x 1x 35x 35x 1x 1x 35x 35x 35x 9x 9x 35x 35x 35x 35x 1x 1x 35x 35x       35x 35x 35x 35x 35x 35x   35x 35x   35x 35x 35x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x         9x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x  
import { isPlatformBrowser } from '@angular/common';
import { AfterViewInit, Component, OnInit, PLATFORM_ID, inject, input, output, viewChild } from '@angular/core';
import { google } from 'google-maps';
import { CityClass } from '@classes/city.class';
import { environment } from '@environments/environment';
 
declare const google: any; // Import Google Maps library if not already done
@Component({
	selector: 'grt-g-map',
	templateUrl: './g-map.component.html',
	styleUrls: ['./g-map.component.scss'],
	standalone: true,
})
export class GMapComponent implements OnInit, AfterViewInit {
	readonly gmapElement = viewChild<any>('gmap');
	readonly removeCity = output<CityClass>();
	readonly addCity = output<CityClass>();
	readonly mapLoaded = output<void>();
	readonly pins = input<any>();
	map: any;
	isBrowser: any;
	availableMarkers: google.maps.Marker[] = [];
	selectedMarkers: google.maps.Marker[] = [];
	constructor() {
		const platformId = inject(PLATFORM_ID);
 
		this.isBrowser = isPlatformBrowser(platformId);
	}
 
	ngOnInit() {
		console.log('Map Initialized');
	}
 
	ngAfterViewInit() {
		if (this.isBrowser) {
			this.loadGoogleMapsScript()
				.then(() => {
					this.initMap();
					this.mapLoaded.emit();
				})
				.catch((error: any) => console.error('Google Maps script could not be loaded.', error));
		}
	}
 
	loadGoogleMapsScript(): Promise<void> {
		return new Promise((resolve, reject) => {
			if (window['google'] && window['google'].maps) {
				resolve();
				return;
			}
			const script = document.createElement('script');
			script.src = `https://maps.googleapis.com/maps/api/js?v=weekly&key=${environment.googleMapsApiKey}`; // Replace YOUR_API_KEY with your actual API key
			script.async = true;
			script.defer = true;
			document.head.appendChild(script);
			script.onload = () => {
				resolve();
			};
			script.onerror = (error) => {
				reject(error);
			};
		});
	}
 
	initMap() {
		const mapProp = {
			center: new google.maps.LatLng(49.374092, 11.561137),
			zoom: 5,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
		};
		this.map = new google.maps.Map(this.gmapElement().nativeElement, mapProp);
 
		const pins = this.pins();
		if (pins) {
			pins.forEach((pin: any) => pin.setMap(this.map));
			this.map.setCenter(pins[0].getPosition());
			this.map.setZoom(18);
		}
	}
 
	public setCenter(index: any) {
		this.map.setCenter(this.pins()[index].getPosition());
	}
 
	focusAsia() {
		if (this.isBrowser) {
			this.map.setCenter(new google.maps.LatLng(14.372079, 102.517827));
			this.map.setZoom(5);
		}
	}
 
	focusEurope() {
		if (this.isBrowser) {
			this.map.setCenter(new google.maps.LatLng(49.374092, 11.561137));
			this.map.setZoom(5);
		}
	}
 
	focusAll() {
		if (this.isBrowser) {
			this.map.setCenter(new google.maps.LatLng(42.49807, 57.083454));
			this.map.setZoom(2);
		}
	}
 
	public drawSelected(cities: CityClass[]) {
		if (this.isBrowser) {
			if (this.selectedMarkers?.length > 0) {
				this.selectedMarkers.forEach((marker) => {
					marker.setMap(null);
				});
			}
			this.selectedMarkers = [];
			const bounds: any = new google.maps.LatLngBounds();
			cities.forEach((city) => {
				const marker = new google.maps.Marker({
					position: new google.maps.LatLng(city.getLat(), city.getLng()),
					icon: {
						url: 'assets/images/pin_checked.svg',
						size: new google.maps.Size(30, 50),
						scaledSize: new google.maps.Size(30, 50),
					},
					map: this.map,
					title: city.name,
				});
				marker.addListener('click', () => {
					this.removeCity.emit(city);
				});
				this.selectedMarkers.push(marker);
				bounds.extend(marker.getPosition());
			});
		}
	}
 
	public drawAvailable(cities: CityClass[]) {
		if (this.isBrowser) {
			console.log('drawing available');
			if (this.availableMarkers?.length > 0) {
				this.availableMarkers.forEach((marker) => {
					marker.setMap(null);
				});
			}
			this.availableMarkers = [];
			const bounds: any = new google.maps.LatLngBounds();
			cities.forEach((city) => {
				const marker = new google.maps.Marker({
					position: new google.maps.LatLng(city.getLat(), city.getLng()),
					icon: {
						url: 'assets/images/pin_unchecked.svg',
						size: new google.maps.Size(30, 50),
						scaledSize: new google.maps.Size(30, 50),
					},
 
					map: this.map,
					title: city.name,
				});
 
				marker.addListener('click', () => {
					this.addCity.emit(city);
				});
				this.availableMarkers.push(marker);
				bounds.extend(marker.getPosition());
			});
		}
	}
}