add pages
This commit is contained in:
@@ -0,0 +1,308 @@
|
||||
import {
|
||||
AfterViewInit,
|
||||
ChangeDetectionStrategy,
|
||||
Component,
|
||||
ElementRef,
|
||||
NgZone,
|
||||
OnDestroy,
|
||||
ViewChild,
|
||||
effect,
|
||||
inject,
|
||||
signal,
|
||||
} from '@angular/core';
|
||||
import { Geolocation, type Position } from '@capacitor/geolocation';
|
||||
import { Store } from '@ngrx/store';
|
||||
|
||||
import { MapItemSheet } from '../map-item-sheet/map-item-sheet';
|
||||
import { CityCatalogService } from '../../root/services/city-catalog.service';
|
||||
import type { Landmark } from '../../data/nizhny-novgorod';
|
||||
import { Icon } from '../../shared/icon/icon';
|
||||
import { selectCollectedIdSet } from '../../state';
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
ymaps3: any;
|
||||
}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-city-map',
|
||||
imports: [Icon, MapItemSheet],
|
||||
templateUrl: './city-map.html',
|
||||
styleUrl: './city-map.scss',
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
})
|
||||
export class CityMap implements AfterViewInit, OnDestroy {
|
||||
@ViewChild('map', { static: true })
|
||||
private mapElement!: ElementRef<HTMLElement>;
|
||||
|
||||
private readonly catalog = inject(CityCatalogService);
|
||||
private readonly store = inject(Store);
|
||||
private readonly zone = inject(NgZone);
|
||||
private map: any;
|
||||
private userMarker: any;
|
||||
private userMarkerElement: HTMLElement | null = null;
|
||||
private watchId: string | null = null;
|
||||
private lastPosition: Position | null = null;
|
||||
private zoomOnNextPan = true;
|
||||
private currentZoom = 13;
|
||||
private readonly markerElements = new Map<string, HTMLElement>();
|
||||
private readonly collectedIds = this.store.selectSignal(selectCollectedIdSet);
|
||||
|
||||
protected readonly city = this.catalog.city;
|
||||
protected readonly selected = signal<Landmark | null>(null);
|
||||
protected readonly following = signal(false);
|
||||
|
||||
constructor() {
|
||||
effect(() => {
|
||||
const ids = this.collectedIds();
|
||||
|
||||
for (const [id, element] of this.markerElements) {
|
||||
this.setMarkerIcon(element, ids.has(id));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async ngAfterViewInit(): Promise<void> {
|
||||
await window.ymaps3.ready;
|
||||
|
||||
const {
|
||||
YMap,
|
||||
YMapDefaultSchemeLayer,
|
||||
YMapDefaultFeaturesLayer,
|
||||
YMapMarker,
|
||||
YMapListener,
|
||||
} = window.ymaps3;
|
||||
const center = this.city.center;
|
||||
|
||||
this.map = new YMap(this.mapElement.nativeElement, {
|
||||
location: {
|
||||
center,
|
||||
zoom: 13,
|
||||
},
|
||||
theme: 'dark',
|
||||
distribution: false,
|
||||
});
|
||||
|
||||
this.map.addChild(new YMapDefaultSchemeLayer({ theme: 'dark' }));
|
||||
this.map.addChild(new YMapDefaultFeaturesLayer());
|
||||
|
||||
if (YMapListener) {
|
||||
this.map.addChild(
|
||||
new YMapListener({
|
||||
onActionStart: (event: { type?: string }) => {
|
||||
if (event?.type === 'drag' || event?.type === 'scrollZoom' || event?.type === 'pinchZoom') {
|
||||
this.zone.run(() => this.following.set(false));
|
||||
}
|
||||
},
|
||||
onUpdate: ({ location }: { location?: { zoom?: number } }) => {
|
||||
if (location?.zoom == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.currentZoom = location.zoom;
|
||||
this.syncRadiusSize();
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
for (const landmark of this.city.objects) {
|
||||
this.map.addChild(this.createMarker(YMapMarker, landmark));
|
||||
}
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
void this.stopWatching();
|
||||
this.map?.destroy();
|
||||
}
|
||||
|
||||
protected focusCity(): void {
|
||||
if (!this.map) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.following.set(false);
|
||||
this.clearSelection();
|
||||
this.map.setLocation({
|
||||
center: this.city.center,
|
||||
zoom: 13,
|
||||
duration: 500,
|
||||
});
|
||||
}
|
||||
|
||||
protected async locate(): Promise<void> {
|
||||
if (!this.map) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.following()) {
|
||||
this.following.set(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.watchId && this.lastPosition) {
|
||||
this.zoomOnNextPan = true;
|
||||
this.following.set(true);
|
||||
this.updateUserPosition(this.lastPosition, true);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const position = await Geolocation.getCurrentPosition({
|
||||
enableHighAccuracy: true,
|
||||
timeout: 30_000,
|
||||
});
|
||||
|
||||
this.zoomOnNextPan = true;
|
||||
this.following.set(true);
|
||||
this.updateUserPosition(position, true);
|
||||
this.watchId = await Geolocation.watchPosition(
|
||||
{ enableHighAccuracy: true },
|
||||
(next, error) => {
|
||||
if (error || !next) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.zone.run(() => this.updateUserPosition(next, this.following()));
|
||||
},
|
||||
);
|
||||
} catch {
|
||||
this.following.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
private updateUserPosition(position: Position, pan: boolean): void {
|
||||
this.lastPosition = position;
|
||||
const coordinates: [number, number] = [position.coords.longitude, position.coords.latitude];
|
||||
const heading = position.coords.heading;
|
||||
|
||||
if (!this.userMarker) {
|
||||
const { YMapMarker } = window.ymaps3;
|
||||
const markerElement = document.createElement('div');
|
||||
markerElement.className = 'city-map__user';
|
||||
markerElement.innerHTML =
|
||||
'<span class="city-map__user-body"><span class="city-map__user-heading"></span><span class="city-map__user-dot"></span></span>';
|
||||
this.userMarkerElement = markerElement;
|
||||
this.userMarker = new YMapMarker({ coordinates, dx: -16, dy: -16 }, markerElement);
|
||||
this.map.addChild(this.userMarker);
|
||||
} else {
|
||||
this.userMarker.update({ coordinates });
|
||||
}
|
||||
|
||||
if (heading != null && !Number.isNaN(heading) && this.userMarkerElement) {
|
||||
this.userMarkerElement.style.setProperty('--heading', `${heading}deg`);
|
||||
this.userMarkerElement.classList.add('city-map__user--headed');
|
||||
}
|
||||
|
||||
if (pan) {
|
||||
this.map.setLocation({
|
||||
center: coordinates,
|
||||
...(this.zoomOnNextPan ? { zoom: 16 } : {}),
|
||||
duration: 400,
|
||||
});
|
||||
this.zoomOnNextPan = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async stopWatching(): Promise<void> {
|
||||
if (!this.watchId) {
|
||||
return;
|
||||
}
|
||||
|
||||
await Geolocation.clearWatch({ id: this.watchId });
|
||||
this.watchId = null;
|
||||
}
|
||||
|
||||
private createMarker(YMapMarker: any, landmark: Landmark): any {
|
||||
const markerElement = document.createElement('button');
|
||||
markerElement.type = 'button';
|
||||
markerElement.className = 'city-map__marker';
|
||||
markerElement.setAttribute('aria-label', landmark.title);
|
||||
markerElement.innerHTML = `<span class="city-map__radius"></span><img width="40" height="40" alt="" /><span class="city-map__label"></span>`;
|
||||
const label = markerElement.querySelector('.city-map__label');
|
||||
|
||||
if (label) {
|
||||
label.textContent = landmark.title;
|
||||
}
|
||||
|
||||
this.markerElements.set(landmark.id, markerElement);
|
||||
this.setMarkerIcon(markerElement, this.collectedIds().has(landmark.id));
|
||||
|
||||
markerElement.addEventListener('click', (event) => {
|
||||
event.stopPropagation();
|
||||
this.zone.run(() => this.selectLandmark(landmark));
|
||||
});
|
||||
|
||||
return new YMapMarker(
|
||||
{
|
||||
coordinates: landmark.coordinates,
|
||||
dx: -20,
|
||||
dy: -20,
|
||||
},
|
||||
markerElement,
|
||||
);
|
||||
}
|
||||
|
||||
private setMarkerIcon(element: HTMLElement, collected: boolean): void {
|
||||
const img = element.querySelector('img');
|
||||
|
||||
if (!img) {
|
||||
return;
|
||||
}
|
||||
|
||||
const src = collected
|
||||
? '/assets/icons/check-marker.svg'
|
||||
: '/assets/icons/lock-marker.svg';
|
||||
|
||||
if (img.getAttribute('src') !== src) {
|
||||
img.setAttribute('src', src);
|
||||
}
|
||||
|
||||
element.classList.toggle('city-map__marker--open', collected);
|
||||
}
|
||||
|
||||
private selectLandmark(landmark: Landmark): void {
|
||||
const alreadySelected = this.selected()?.id === landmark.id;
|
||||
this.selected.set(alreadySelected ? null : landmark);
|
||||
this.syncSelection();
|
||||
this.syncRadiusSize();
|
||||
}
|
||||
|
||||
private clearSelection(): void {
|
||||
this.selected.set(null);
|
||||
this.syncSelection();
|
||||
}
|
||||
|
||||
private syncSelection(): void {
|
||||
const selectedId = this.selected()?.id;
|
||||
|
||||
for (const [id, element] of this.markerElements) {
|
||||
element.classList.toggle('city-map__marker--active', id === selectedId);
|
||||
}
|
||||
}
|
||||
|
||||
private syncRadiusSize(): void {
|
||||
const selected = this.selected();
|
||||
|
||||
if (!selected) {
|
||||
return;
|
||||
}
|
||||
|
||||
const element = this.markerElements.get(selected.id);
|
||||
|
||||
if (!element) {
|
||||
return;
|
||||
}
|
||||
|
||||
const diameter = this.metersToPixels(selected.geoRadiusMeters, selected.coordinates[1]) * 2;
|
||||
element.style.setProperty('--radius-px', `${diameter}px`);
|
||||
}
|
||||
|
||||
private metersToPixels(meters: number, latitude: number): number {
|
||||
const metersPerPixel =
|
||||
(156543.03392 * Math.cos((latitude * Math.PI) / 180)) / 2 ** this.currentZoom;
|
||||
|
||||
return meters / Math.max(metersPerPixel, 0.01);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user