add logic
This commit is contained in:
@@ -15,13 +15,13 @@ import {
|
||||
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 type { Attraction } from '../../areas/area.models';
|
||||
import { Icon } from '../../shared/icon/icon';
|
||||
import { selectCollectedIdSet } from '../../state';
|
||||
import { MapItemSheet } from '../map-item-sheet/map-item-sheet';
|
||||
|
||||
const MAP_KEY = 'bf0a466f-43a6-48c7-b274-7fea5952957b';
|
||||
const CITY_CENTER: [number, number] = [43.99934, 56.329399];
|
||||
const CITY_ZOOM = 16;
|
||||
const USER_ZOOM = 20;
|
||||
const LANDMARK_SIZE = 40;
|
||||
@@ -34,6 +34,7 @@ interface DgPoint {
|
||||
|
||||
interface DgMarker {
|
||||
addTo(map: DgMap): this;
|
||||
remove(): void;
|
||||
setLatLng(latLng: [number, number]): this;
|
||||
setZIndexOffset(offset: number): this;
|
||||
getElement(): HTMLElement | undefined;
|
||||
@@ -135,7 +136,6 @@ 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 raster: DgApi | null = null;
|
||||
@@ -152,6 +152,8 @@ export class CityMap implements AfterViewInit, OnDestroy {
|
||||
private readonly clippedAncestors: HTMLElement[] = [];
|
||||
private readonly markerElements = new Map<string, HTMLElement>();
|
||||
private readonly markers = new Map<string, DgMarker>();
|
||||
private readonly mapReady = signal(false);
|
||||
private placedKey = '';
|
||||
private readonly collectedIds = this.store.selectSignal(selectCollectedIdSet);
|
||||
private readonly stopFollowing = (): void => {
|
||||
this.programmaticMove = false;
|
||||
@@ -169,19 +171,24 @@ export class CityMap implements AfterViewInit, OnDestroy {
|
||||
};
|
||||
|
||||
readonly embedded = input(false);
|
||||
readonly cityName = input('');
|
||||
readonly areaId = input('');
|
||||
readonly attractions = input<Attraction[]>([]);
|
||||
|
||||
protected readonly city = this.catalog.city;
|
||||
protected readonly selected = signal<Landmark | null>(null);
|
||||
protected readonly selected = signal<Attraction | null>(null);
|
||||
protected readonly following = signal(false);
|
||||
protected readonly fullscreen = signal(false);
|
||||
|
||||
constructor() {
|
||||
effect(() => {
|
||||
const items = this.attractions();
|
||||
const ids = this.collectedIds();
|
||||
|
||||
for (const [id, element] of this.markerElements) {
|
||||
this.setMarkerIcon(element, ids.has(id));
|
||||
if (!this.mapReady()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.placeMarkers(items, ids);
|
||||
});
|
||||
|
||||
effect(() => {
|
||||
@@ -210,7 +217,7 @@ export class CityMap implements AfterViewInit, OnDestroy {
|
||||
}
|
||||
|
||||
const map = raster.map(this.mapElement.nativeElement, {
|
||||
center: this.toLatLng(this.city.center),
|
||||
center: this.toLatLng(this.focusCenter(this.attractions())),
|
||||
zoom: CITY_ZOOM,
|
||||
key: MAP_KEY,
|
||||
currentLang: 'ru',
|
||||
@@ -228,14 +235,9 @@ export class CityMap implements AfterViewInit, OnDestroy {
|
||||
this.map = map;
|
||||
map.on('dragstart', this.stopFollowing);
|
||||
map.on('zoomstart', this.onZoomStart);
|
||||
map.on('zoom', () => this.syncRadiusSize());
|
||||
map.on('move', () => this.syncRadiusSize());
|
||||
map.on('moveend', this.onMoveEnd);
|
||||
|
||||
for (const landmark of this.city.objects) {
|
||||
this.markers.set(landmark.id, this.createMarker(raster, map, landmark));
|
||||
}
|
||||
|
||||
this.zone.run(() => this.mapReady.set(true));
|
||||
requestAnimationFrame(() => this.resizeMap());
|
||||
});
|
||||
}
|
||||
@@ -268,7 +270,7 @@ export class CityMap implements AfterViewInit, OnDestroy {
|
||||
protected focusCity(): void {
|
||||
this.following.set(false);
|
||||
this.clearSelection();
|
||||
this.moveMap(this.city.center, CITY_ZOOM, 500);
|
||||
this.moveMap(this.focusCenter(this.attractions()), CITY_ZOOM, 500);
|
||||
}
|
||||
|
||||
protected async locate(): Promise<void> {
|
||||
@@ -364,25 +366,69 @@ export class CityMap implements AfterViewInit, OnDestroy {
|
||||
this.watchId = null;
|
||||
}
|
||||
|
||||
private createMarker(api: DgApi, map: DgMap, landmark: Landmark): DgMarker {
|
||||
private placeMarkers(items: Attraction[], collectedIds: Set<string>): void {
|
||||
const map = this.map;
|
||||
const api = this.raster;
|
||||
|
||||
if (!map || !api) {
|
||||
return;
|
||||
}
|
||||
|
||||
const key = items.map((item) => `${item.id}:${item.latitude}:${item.longitude}`).join('|');
|
||||
|
||||
if (key === this.placedKey) {
|
||||
for (const item of items) {
|
||||
const element = this.markerElements.get(item.id);
|
||||
|
||||
if (element) {
|
||||
this.setMarkerIcon(element, collectedIds.has(item.id) || item.visited);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
for (const marker of this.markers.values()) {
|
||||
marker.remove();
|
||||
}
|
||||
|
||||
this.markers.clear();
|
||||
this.markerElements.clear();
|
||||
this.placedKey = key;
|
||||
|
||||
for (const item of items) {
|
||||
if (typeof item.latitude !== 'number' || typeof item.longitude !== 'number') {
|
||||
continue;
|
||||
}
|
||||
|
||||
this.markers.set(item.id, this.createMarker(api, map, item, collectedIds.has(item.id) || item.visited));
|
||||
}
|
||||
|
||||
if (this.markers.size > 0) {
|
||||
this.moveMap(this.focusCenter(items), CITY_ZOOM, 0);
|
||||
}
|
||||
}
|
||||
|
||||
private createMarker(api: DgApi, map: DgMap, landmark: Attraction, collected: boolean): DgMarker {
|
||||
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>`;
|
||||
markerElement.setAttribute('aria-label', landmark.name);
|
||||
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;
|
||||
label.textContent = landmark.name;
|
||||
}
|
||||
|
||||
this.markerElements.set(landmark.id, markerElement);
|
||||
this.setMarkerIcon(markerElement, this.collectedIds().has(landmark.id));
|
||||
this.setMarkerIcon(markerElement, collected);
|
||||
|
||||
const marker = this.addHtmlMarker(
|
||||
api,
|
||||
map,
|
||||
landmark.coordinates,
|
||||
[landmark.longitude, landmark.latitude],
|
||||
markerElement,
|
||||
LANDMARK_SIZE,
|
||||
1,
|
||||
@@ -396,6 +442,21 @@ export class CityMap implements AfterViewInit, OnDestroy {
|
||||
return marker;
|
||||
}
|
||||
|
||||
private focusCenter(items: Attraction[]): [number, number] {
|
||||
const points = items.filter(
|
||||
(item) => typeof item.latitude === 'number' && typeof item.longitude === 'number',
|
||||
);
|
||||
|
||||
if (points.length === 0) {
|
||||
return CITY_CENTER;
|
||||
}
|
||||
|
||||
const longitude = points.reduce((sum, item) => sum + item.longitude, 0) / points.length;
|
||||
const latitude = points.reduce((sum, item) => sum + item.latitude, 0) / points.length;
|
||||
|
||||
return [longitude, latitude];
|
||||
}
|
||||
|
||||
private addHtmlMarker(
|
||||
api: DgApi,
|
||||
map: DgMap,
|
||||
@@ -443,11 +504,10 @@ export class CityMap implements AfterViewInit, OnDestroy {
|
||||
element.classList.toggle('city-map__marker--open', collected);
|
||||
}
|
||||
|
||||
private selectLandmark(landmark: Landmark): void {
|
||||
private selectLandmark(landmark: Attraction): void {
|
||||
const alreadySelected = this.selected()?.id === landmark.id;
|
||||
this.selected.set(alreadySelected ? null : landmark);
|
||||
this.syncSelection();
|
||||
this.syncRadiusSize();
|
||||
}
|
||||
|
||||
private clearSelection(): void {
|
||||
@@ -465,36 +525,6 @@ export class CityMap implements AfterViewInit, OnDestroy {
|
||||
}
|
||||
}
|
||||
|
||||
private syncRadiusSize(): void {
|
||||
const selected = this.selected();
|
||||
const map = this.map;
|
||||
|
||||
if (!selected || !map) {
|
||||
return;
|
||||
}
|
||||
|
||||
const element = this.markerElements.get(selected.id);
|
||||
|
||||
if (!element) {
|
||||
return;
|
||||
}
|
||||
|
||||
const diameter = this.metersToPixels(map, selected.geoRadiusMeters, selected.coordinates) * 2;
|
||||
element.style.setProperty('--radius-px', `${diameter}px`);
|
||||
}
|
||||
|
||||
private metersToPixels(map: DgMap, meters: number, coordinates: [number, number]): number {
|
||||
const [longitude, latitude] = coordinates;
|
||||
const origin = map.latLngToContainerPoint(this.toLatLng(coordinates));
|
||||
const metersPerDegree = 111_320 * Math.cos((latitude * Math.PI) / 180);
|
||||
const shifted = map.latLngToContainerPoint([
|
||||
latitude,
|
||||
longitude + meters / Math.max(metersPerDegree, 1),
|
||||
]);
|
||||
|
||||
return Math.hypot(shifted.x - origin.x, shifted.y - origin.y);
|
||||
}
|
||||
|
||||
private moveMap(center: [number, number], zoom: number | null, durationMs: number): void {
|
||||
const map = this.map;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user