diff --git a/package-lock.json b/package-lock.json index ab24165..1cb663a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,6 @@ "name": "gorodar-front", "version": "0.0.0", "dependencies": { - "@2gis/mapgl": "^1.78.1", "@angular/animations": "^20.3.32", "@angular/cdk": "^20.2.14", "@angular/common": "^20.3.0", @@ -45,18 +44,6 @@ "typescript": "~5.9.2" } }, - "node_modules/@2gis/mapgl": { - "version": "1.78.1", - "resolved": "https://registry.npmjs.org/@2gis/mapgl/-/mapgl-1.78.1.tgz", - "integrity": "sha512-SkbrqwBMPTLV4hihV+BgAtftIfyMeSru6yqlVF0IJEE6jaGQ8LFq0NJb1TTaXt3I1t3q8sjvU7X/hhHpxo5JXQ==", - "license": "BSD-2-Clause", - "dependencies": { - "@types/geojson": "^7946.0.7" - }, - "engines": { - "node": ">=16.0.0" - } - }, "node_modules/@algolia/abtesting": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@algolia/abtesting/-/abtesting-1.1.0.tgz", @@ -4548,12 +4535,6 @@ "@types/node": "*" } }, - "node_modules/@types/geojson": { - "version": "7946.0.16", - "resolved": "https://registry.npmjs.org/@types/geojson/-/geojson-7946.0.16.tgz", - "integrity": "sha512-6C8nqWur3j98U6+lXDfTUWIfgvZU+EumvpHKcYjujKH7woYyLj2sUmff0tRhrqM7BohUw7Pz3ZB1jj2gW9Fvmg==", - "license": "MIT" - }, "node_modules/@types/jasmine": { "version": "5.1.15", "resolved": "https://registry.npmjs.org/@types/jasmine/-/jasmine-5.1.15.tgz", diff --git a/package.json b/package.json index 58320d7..2154bc1 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ }, "private": true, "dependencies": { - "@2gis/mapgl": "^1.78.1", "@angular/animations": "^20.3.32", "@angular/cdk": "^20.2.14", "@angular/common": "^20.3.0", diff --git a/src/app/map/city-map/city-map.ts b/src/app/map/city-map/city-map.ts index 25ff4d3..77f689b 100644 --- a/src/app/map/city-map/city-map.ts +++ b/src/app/map/city-map/city-map.ts @@ -10,7 +10,6 @@ import { inject, signal, } from '@angular/core'; -import { load } from '@2gis/mapgl'; import { Geolocation, type Position } from '@capacitor/geolocation'; import { Store } from '@ngrx/store'; @@ -20,16 +19,104 @@ import type { Landmark } from '../../data/nizhny-novgorod'; import { Icon } from '../../shared/icon/icon'; import { selectCollectedIdSet } from '../../state'; -const MAPGL_KEY = 'bf0a466f-43a6-48c7-b274-7fea5952957b'; -const MAP_STYLE_NIGHT = 'e05ac437-fcc2-4845-ad74-b1de9ce07555'; +const MAP_KEY = 'bf0a466f-43a6-48c7-b274-7fea5952957b'; const CITY_ZOOM = 13; const USER_ZOOM = 16; const LANDMARK_SIZE = 40; const USER_SIZE = 32; -type MapglApi = Awaited>; -type MapglMap = InstanceType; -type MapglHtmlMarker = InstanceType; +interface DgPoint { + x: number; + y: number; +} + +interface DgMarker { + addTo(map: DgMap): this; + setLatLng(latLng: [number, number]): this; + setZIndexOffset(offset: number): this; + getElement(): HTMLElement | undefined; + on(type: string, handler: (event: Event) => void): this; +} + +interface DgMap { + on(type: string, handler: () => void): this; + off(type: string, handler: () => void): this; + setView( + center: [number, number], + zoom: number, + options?: { animate?: boolean; duration?: number }, + ): this; + getZoom(): number; + latLngToContainerPoint(latLng: [number, number]): DgPoint; + remove(): void; +} + +interface DgLoader { + then(resolve: () => void, reject?: (error: unknown) => void): Promise; +} + +interface DgApi { + map(element: HTMLElement, options: Record): DgMap; + marker(latLng: [number, number], options: Record): DgMarker; + divIcon(options: Record): unknown; +} + +declare global { + interface Window { + DG?: DgLoader & DgApi; + } +} + +let rasterApi: Promise | null = null; + +function loadRasterApi(): Promise { + if (rasterApi) { + return rasterApi; + } + + rasterApi = new Promise((resolve, reject) => { + const ready = (): void => { + const api = window.DG; + + if (!api) { + reject(new Error('RasterJS API не загрузился')); + return; + } + + void api.then( + () => { + const readyApi = window.DG; + + if (!readyApi?.map || !readyApi.marker || !readyApi.divIcon) { + reject(new Error('RasterJS API не загрузился')); + return; + } + + resolve({ + map: readyApi.map.bind(readyApi), + marker: readyApi.marker.bind(readyApi), + divIcon: readyApi.divIcon.bind(readyApi), + }); + }, + (error: unknown) => reject(error), + ); + }; + + if (window.DG) { + ready(); + return; + } + + const script = document.createElement('script'); + script.src = `https://maps.api.2gis.ru/2.0/loader.js?pkg=full&lazy=true&skin=dark&key=${MAP_KEY}`; + script.async = true; + script.onload = ready; + script.onerror = () => reject(new Error('Не удалось загрузить RasterJS API')); + document.body.appendChild(script); + }); + + return rasterApi; +} @Component({ selector: 'app-city-map', @@ -45,17 +132,32 @@ export class CityMap implements AfterViewInit, OnDestroy { private readonly catalog = inject(CityCatalogService); private readonly store = inject(Store); private readonly zone = inject(NgZone); - private mapgl: MapglApi | null = null; - private map: MapglMap | null = null; - private userMarker: MapglHtmlMarker | null = null; + private raster: DgApi | null = null; + private map: DgMap | null = null; + private userMarker: DgMarker | null = null; private userMarkerElement: HTMLElement | null = null; private watchId: string | null = null; private lastPosition: Position | null = null; private zoomOnNextPan = true; private destroyed = false; + private programmaticMove = false; private readonly markerElements = new Map(); - private readonly markers = new Map(); + private readonly markers = new Map(); private readonly collectedIds = this.store.selectSignal(selectCollectedIdSet); + private readonly stopFollowing = (): void => { + this.programmaticMove = false; + this.zone.run(() => this.following.set(false)); + }; + private readonly onZoomStart = (): void => { + if (this.programmaticMove) { + return; + } + + this.stopFollowing(); + }; + private readonly onMoveEnd = (): void => { + this.programmaticMove = false; + }; protected readonly city = this.catalog.city; protected readonly selected = signal(null); @@ -72,49 +174,45 @@ export class CityMap implements AfterViewInit, OnDestroy { } async ngAfterViewInit(): Promise { - const mapgl = await load(); + const api = await loadRasterApi(); + this.raster = api; if (this.destroyed) { return; } - this.mapgl = mapgl; - this.zone.runOutsideAngular(() => { - if (this.destroyed) { + const raster = this.raster; + + if (this.destroyed || !raster) { return; } - const map = new mapgl.Map(this.mapElement.nativeElement, { - center: this.city.center, + const map = raster.map(this.mapElement.nativeElement, { + center: this.toLatLng(this.city.center), zoom: CITY_ZOOM, - key: MAPGL_KEY, - lang: 'ru', - style: MAP_STYLE_NIGHT, - defaultBackgroundColor: '#12110f', + key: MAP_KEY, + currentLang: 'ru', zoomControl: false, - disablePitchByUserInteraction: true, - disableRotationByUserInteraction: true, - enableTrackResize: true, + fullscreenControl: false, + poi: false, + geoclicker: false, }); if (this.destroyed) { - map.destroy(); + map.remove(); return; } this.map = map; - map.on('movestart', (event) => { - if (!event.isUser) { - return; - } - - this.zone.run(() => this.following.set(false)); - }); + 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(mapgl, map, landmark)); + this.markers.set(landmark.id, this.createMarker(raster, map, landmark)); } }); } @@ -122,7 +220,10 @@ export class CityMap implements AfterViewInit, OnDestroy { ngOnDestroy(): void { this.destroyed = true; void this.stopWatching(); - this.map?.destroy(); + this.map?.off('dragstart', this.stopFollowing); + this.map?.off('zoomstart', this.onZoomStart); + this.map?.off('moveend', this.onMoveEnd); + this.map?.remove(); this.map = null; } @@ -184,25 +285,25 @@ export class CityMap implements AfterViewInit, OnDestroy { const coordinates: [number, number] = [position.coords.longitude, position.coords.latitude]; const heading = position.coords.heading; - if (!this.userMarker && this.mapgl) { + const raster = this.raster; + + if (!this.userMarker && raster) { const markerElement = document.createElement('div'); markerElement.className = 'city-map__user'; markerElement.innerHTML = ''; this.userMarkerElement = markerElement; - this.userMarker = new this.mapgl.HtmlMarker(map, { + this.userMarker = this.addHtmlMarker( + raster, + 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'; + markerElement, + USER_SIZE, + 3, + false, + ); } else { - this.userMarker?.setCoordinates(coordinates); + this.userMarker?.setLatLng(this.toLatLng(coordinates)); } if (heading != null && !Number.isNaN(heading) && this.userMarkerElement) { @@ -225,7 +326,7 @@ export class CityMap implements AfterViewInit, OnDestroy { this.watchId = null; } - private createMarker(mapgl: MapglApi, map: MapglMap, landmark: Landmark): MapglHtmlMarker { + private createMarker(api: DgApi, map: DgMap, landmark: Landmark): DgMarker { const markerElement = document.createElement('button'); markerElement.type = 'button'; markerElement.className = 'city-map__marker'; @@ -240,21 +341,50 @@ export class CityMap implements AfterViewInit, OnDestroy { this.markerElements.set(landmark.id, markerElement); this.setMarkerIcon(markerElement, this.collectedIds().has(landmark.id)); - markerElement.addEventListener('click', (event) => { - event.stopPropagation(); + const marker = this.addHtmlMarker( + api, + map, + landmark.coordinates, + markerElement, + LANDMARK_SIZE, + 1, + true, + ); + + marker.on('click', () => { this.zone.run(() => this.selectLandmark(landmark)); }); - 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, + return marker; + } + + private addHtmlMarker( + api: DgApi, + map: DgMap, + coordinates: [number, number], + element: HTMLElement, + size: number, + zIndexOffset: number, + interactive: boolean, + ): DgMarker { + const marker = api.marker(this.toLatLng(coordinates), { + icon: api.divIcon({ + className: 'city-map__pin', + html: element, + iconSize: [size, size], + iconAnchor: [size / 2, size / 2], + }), + interactive, + keyboard: false, + zIndexOffset, }); - marker.getContent().style.overflow = 'visible'; + + marker.addTo(map); + const root = marker.getElement(); + + if (root) { + root.style.overflow = 'visible'; + } return marker; } @@ -293,7 +423,7 @@ export class CityMap implements AfterViewInit, OnDestroy { for (const [id, element] of this.markerElements) { const active = id === selectedId; element.classList.toggle('city-map__marker--active', active); - this.markers.get(id)?.setZIndex(active ? 2 : 1); + this.markers.get(id)?.setZIndexOffset(active ? 2 : 1); } } @@ -315,37 +445,33 @@ export class CityMap implements AfterViewInit, OnDestroy { element.style.setProperty('--radius-px', `${diameter}px`); } - private metersToPixels( - map: MapglMap, - meters: number, - coordinates: [number, number], - ): number { + private metersToPixels(map: DgMap, meters: number, coordinates: [number, number]): number { const [longitude, latitude] = coordinates; - const origin = map.project(coordinates); + const origin = map.latLngToContainerPoint(this.toLatLng(coordinates)); const metersPerDegree = 111_320 * Math.cos((latitude * Math.PI) / 180); - const shifted = map.project([ - longitude + meters / Math.max(metersPerDegree, 1), + const shifted = map.latLngToContainerPoint([ latitude, + longitude + meters / Math.max(metersPerDegree, 1), ]); - return Math.hypot(shifted[0] - origin[0], shifted[1] - origin[1]); + return Math.hypot(shifted.x - origin.x, shifted.y - origin.y); } - private moveMap(center: [number, number], zoom: number | null, duration: number): void { + private moveMap(center: [number, number], zoom: number | null, durationMs: number): void { const map = this.map; if (!map) { return; } - map.setCenter(center, { duration, easing: 'easeOutCubic' }); + this.programmaticMove = true; + map.setView(this.toLatLng(center), zoom ?? map.getZoom(), { + animate: true, + duration: durationMs / 1000, + }); + } - if (zoom != null) { - map.setZoom(zoom, { - duration, - easing: 'easeOutCubic', - useHeightForAnimation: true, - }); - } + private toLatLng(coordinates: [number, number]): [number, number] { + return [coordinates[1], coordinates[0]]; } } diff --git a/src/assets/styles/_map.scss b/src/assets/styles/_map.scss index 04680e5..39a7ccc 100644 --- a/src/assets/styles/_map.scss +++ b/src/assets/styles/_map.scss @@ -140,7 +140,24 @@ } } -.city-map__canvas :has(> .city-map__marker), -.city-map__canvas :has(> .city-map__user) { - overflow: visible !important; +.city-map__canvas .leaflet-container { + width: 100%; + height: 100%; + background: #12110f; + font: inherit; +} + +.city-map__canvas .leaflet-tile-pane { + filter: invert(1) hue-rotate(180deg) brightness(0.92) contrast(0.92) saturate(0.65); +} + +.city-map__canvas .leaflet-div-icon, +.city-map__pin { + background: transparent; + border: 0; +} + +.city-map__canvas .leaflet-marker-icon, +.city-map__canvas .leaflet-marker-pane { + overflow: visible; }