update for 2gis

This commit is contained in:
Artem
2026-09-30 10:38:43 +03:00
parent 7e8cb26ddd
commit deceebd899
13 changed files with 296 additions and 253 deletions
+139 -96
View File
@@ -10,6 +10,7 @@ import {
inject,
signal,
} from '@angular/core';
import { load } from '@2gis/mapgl';
import { Geolocation, type Position } from '@capacitor/geolocation';
import { Store } from '@ngrx/store';
@@ -19,11 +20,16 @@ import type { Landmark } from '../../data/nizhny-novgorod';
import { Icon } from '../../shared/icon/icon';
import { selectCollectedIdSet } from '../../state';
declare global {
interface Window {
ymaps3: any;
}
}
const MAPGL_KEY = 'bf0a466f-43a6-48c7-b274-7fea5952957b';
const MAP_STYLE_NIGHT = 'e05ac437-fcc2-4845-ad74-b1de9ce07555';
const CITY_ZOOM = 13;
const USER_ZOOM = 16;
const LANDMARK_SIZE = 40;
const USER_SIZE = 32;
type MapglApi = Awaited<ReturnType<typeof load>>;
type MapglMap = InstanceType<MapglApi['Map']>;
type MapglHtmlMarker = InstanceType<MapglApi['HtmlMarker']>;
@Component({
selector: 'app-city-map',
@@ -39,14 +45,16 @@ export class CityMap implements AfterViewInit, OnDestroy {
private readonly catalog = inject(CityCatalogService);
private readonly store = inject(Store);
private readonly zone = inject(NgZone);
private map: any;
private userMarker: any;
private mapgl: MapglApi | null = null;
private map: MapglMap | null = null;
private userMarker: MapglHtmlMarker | null = null;
private userMarkerElement: HTMLElement | null = null;
private watchId: string | null = null;
private lastPosition: Position | null = null;
private zoomOnNextPan = true;
private currentZoom = 13;
private destroyed = false;
private readonly markerElements = new Map<string, HTMLElement>();
private readonly markers = new Map<string, MapglHtmlMarker>();
private readonly collectedIds = this.store.selectSignal(selectCollectedIdSet);
protected readonly city = this.catalog.city;
@@ -64,71 +72,64 @@ export class CityMap implements AfterViewInit, OnDestroy {
}
async ngAfterViewInit(): Promise<void> {
await window.ymaps3.ready;
const mapgl = await load();
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) {
if (this.destroyed) {
return;
}
this.mapgl = mapgl;
this.zone.runOutsideAngular(() => {
if (this.destroyed) {
return;
}
const map = new mapgl.Map(this.mapElement.nativeElement, {
center: this.city.center,
zoom: CITY_ZOOM,
key: MAPGL_KEY,
lang: 'ru',
style: MAP_STYLE_NIGHT,
defaultBackgroundColor: '#12110f',
zoomControl: false,
disablePitchByUserInteraction: true,
disableRotationByUserInteraction: true,
enableTrackResize: true,
});
if (this.destroyed) {
map.destroy();
return;
}
this.map = map;
map.on('movestart', (event) => {
if (!event.isUser) {
return;
}
this.zone.run(() => this.following.set(false));
});
map.on('move', () => this.syncRadiusSize());
for (const landmark of this.city.objects) {
this.markers.set(landmark.id, this.createMarker(mapgl, map, landmark));
}
});
}
ngOnDestroy(): void {
this.destroyed = true;
void this.stopWatching();
this.map?.destroy();
this.map = null;
}
protected focusCity(): void {
this.following.set(false);
this.clearSelection();
this.map.setLocation({
center: this.city.center,
zoom: 13,
duration: 500,
});
this.moveMap(this.city.center, CITY_ZOOM, 500);
}
protected async locate(): Promise<void> {
@@ -173,21 +174,35 @@ export class CityMap implements AfterViewInit, OnDestroy {
}
private updateUserPosition(position: Position, pan: boolean): void {
const map = this.map;
if (!map) {
return;
}
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;
if (!this.userMarker && this.mapgl) {
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);
this.userMarker = new this.mapgl.HtmlMarker(map, {
coordinates,
html: markerElement,
anchor: [USER_SIZE / 2, USER_SIZE / 2],
interactive: false,
preventMapInteractions: false,
disableRounding: true,
labeling: { type: 'none' },
zIndex: 3,
});
this.userMarker.getContent().style.overflow = 'visible';
} else {
this.userMarker.update({ coordinates });
this.userMarker?.setCoordinates(coordinates);
}
if (heading != null && !Number.isNaN(heading) && this.userMarkerElement) {
@@ -196,11 +211,7 @@ export class CityMap implements AfterViewInit, OnDestroy {
}
if (pan) {
this.map.setLocation({
center: coordinates,
...(this.zoomOnNextPan ? { zoom: 16 } : {}),
duration: 400,
});
this.moveMap(coordinates, this.zoomOnNextPan ? USER_ZOOM : null, 400);
this.zoomOnNextPan = false;
}
}
@@ -214,7 +225,7 @@ export class CityMap implements AfterViewInit, OnDestroy {
this.watchId = null;
}
private createMarker(YMapMarker: any, landmark: Landmark): any {
private createMarker(mapgl: MapglApi, map: MapglMap, landmark: Landmark): MapglHtmlMarker {
const markerElement = document.createElement('button');
markerElement.type = 'button';
markerElement.className = 'city-map__marker';
@@ -234,14 +245,18 @@ export class CityMap implements AfterViewInit, OnDestroy {
this.zone.run(() => this.selectLandmark(landmark));
});
return new YMapMarker(
{
coordinates: landmark.coordinates,
dx: -20,
dy: -20,
},
markerElement,
);
const marker = new mapgl.HtmlMarker(map, {
coordinates: landmark.coordinates,
html: markerElement,
anchor: [LANDMARK_SIZE / 2, LANDMARK_SIZE / 2],
interactive: true,
disableRounding: true,
labeling: { type: 'none' },
zIndex: 1,
});
marker.getContent().style.overflow = 'visible';
return marker;
}
private setMarkerIcon(element: HTMLElement, collected: boolean): void {
@@ -251,9 +266,7 @@ export class CityMap implements AfterViewInit, OnDestroy {
return;
}
const src = collected
? '/assets/icons/check-marker.svg'
: '/assets/icons/lock-marker.svg';
const src = collected ? '/assets/icons/check-marker.svg' : '/assets/icons/lock-marker.svg';
if (img.getAttribute('src') !== src) {
img.setAttribute('src', src);
@@ -278,14 +291,17 @@ export class CityMap implements AfterViewInit, OnDestroy {
const selectedId = this.selected()?.id;
for (const [id, element] of this.markerElements) {
element.classList.toggle('city-map__marker--active', id === selectedId);
const active = id === selectedId;
element.classList.toggle('city-map__marker--active', active);
this.markers.get(id)?.setZIndex(active ? 2 : 1);
}
}
private syncRadiusSize(): void {
const selected = this.selected();
const map = this.map;
if (!selected) {
if (!selected || !map) {
return;
}
@@ -295,14 +311,41 @@ export class CityMap implements AfterViewInit, OnDestroy {
return;
}
const diameter = this.metersToPixels(selected.geoRadiusMeters, selected.coordinates[1]) * 2;
const diameter = this.metersToPixels(map, selected.geoRadiusMeters, selected.coordinates) * 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;
private metersToPixels(
map: MapglMap,
meters: number,
coordinates: [number, number],
): number {
const [longitude, latitude] = coordinates;
const origin = map.project(coordinates);
const metersPerDegree = 111_320 * Math.cos((latitude * Math.PI) / 180);
const shifted = map.project([
longitude + meters / Math.max(metersPerDegree, 1),
latitude,
]);
return meters / Math.max(metersPerPixel, 0.01);
return Math.hypot(shifted[0] - origin[0], shifted[1] - origin[1]);
}
private moveMap(center: [number, number], zoom: number | null, duration: number): void {
const map = this.map;
if (!map) {
return;
}
map.setCenter(center, { duration, easing: 'easeOutCubic' });
if (zoom != null) {
map.setZoom(zoom, {
duration,
easing: 'easeOutCubic',
useHeightForAnimation: true,
});
}
}
}