11import round from 'lodash/round' ;
22import mapboxgl from 'mapbox-gl' ;
3+ import * as maplibregl from 'maplibre-gl' ;
4+ import 'maplibre-gl/dist/maplibre-gl.css' ;
5+ import { Protocol as PmTilesProtocol } from 'pmtiles' ;
36import React from 'react' ;
47
8+ import { useFeatureFlag } from 'screens/App/shared/stores/FeatureFlagsStore' ;
9+ import FeatureFlag from 'screens/App/shared/types/FeatureFlag' ;
10+
511import useCorrectionsStore , {
612 useCorrectionsStoreComputeds ,
713} from '../stores/CorrectionsStore' ;
@@ -14,15 +20,24 @@ import {
1420} from '../../MapPane/components/MainMap/overlays' ;
1521import stylesheet from './LocationPickerModal.less' ;
1622
23+ import mapStyleUrl from 'screens/App/shared/mapStyles/fourties.protomaps.style.json' ;
24+
1725const MAPBOX_STYLE = __DEV__
1826 ? 'mapbox://styles/julianboilen/ck5jrzrs11r1p1imia7qzjkm1/draft'
1927 : 'mapbox://styles/julianboilen/ck5jrzrs11r1p1imia7qzjkm1' ;
2028
2129const DEFAULT_LNG_LAT = [ - 73.99397 , 40.7093 ] as const ;
2230
31+ // Initialize pmtiles protocol for MapLibre
32+ const pmtilesProtocol = new PmTilesProtocol ( ) ;
33+ if ( typeof maplibregl !== 'undefined' && maplibregl . addProtocol ) {
34+ maplibregl . addProtocol ( 'pmtiles' , pmtilesProtocol . tile ) ;
35+ }
36+
2337export default function LocationPickerModal ( ) : JSX . Element {
38+ const useMapLibre = useFeatureFlag ( FeatureFlag . USE_MAPLIBRE ) ;
2439 const mapContainer = React . useRef < HTMLDivElement > ( null ) ;
25- const map = React . useRef < mapboxgl . Map | null > ( null ) ;
40+ const map = React . useRef < mapboxgl . Map | maplibregl . Map | null > ( null ) ;
2641 const {
2742 isMapOpen,
2843 closeMap,
@@ -54,50 +69,106 @@ export default function LocationPickerModal(): JSX.Element {
5469 correctedLng ?? previousLng ?? DEFAULT_LNG_LAT [ 0 ] ,
5570 correctedLat ?? previousLat ?? DEFAULT_LNG_LAT [ 1 ] ,
5671 ] as [ number , number ] ;
57- map . current = new mapboxgl . Map ( {
58- container : mapContainer . current ,
59- style : MAPBOX_STYLE ,
60- center : startingPosition ,
61- maxBounds : [
62- [ - 74.25908989999999 , 40.4773991 ] , // SW
63- [ - 73.70027209999999 , 40.9175771 ] , // NE
64- ] ,
65- zoom : 17 ,
66- hash : false ,
67- } ) ;
68-
69- map . current . on ( 'style.load' , ( ) => {
70- // map.current.removeLayer('photos-1940s');
71- map . current . setFilter ( 'photos-1940s' , [
72- '!=' ,
73- [ 'get' , 'photoIdentifier' ] ,
74- photo . identifier || null ,
75- ] ) ;
76- map . current . removeLayer ( 'photos-1940s-wide-zoom' ) ;
77-
78- map . current . addControl ( new mapboxgl . NavigationControl ( ) , 'top-right' ) ;
79- installLayers ( map . current , 'photos-1940s' , {
80- fadeOverlays : false ,
72+
73+ if ( useMapLibre ) {
74+ // MapLibre implementation
75+ map . current = new maplibregl . Map ( {
76+ container : mapContainer . current ,
77+ style : mapStyleUrl as unknown as string ,
78+ center : startingPosition ,
79+ maxBounds : [
80+ [ - 74.25908989999999 , 40.4773991 ] , // SW
81+ [ - 73.70027209999999 , 40.9175771 ] , // NE
82+ ] ,
83+ zoom : 17 ,
84+ hash : false ,
85+ attributionControl : {
86+ compact : false ,
87+ } ,
8188 } ) ;
82- setOverlay ( map . current , [ 'default-map' , 'bbl-label' ] ) ;
83- } ) ;
84-
85- map . current . on ( 'moveend' , ( ) => {
86- const center = map . current . getCenter ( ) ;
87- setCorrectedLngLat ( round ( center . lng , 6 ) , round ( center . lat , 6 ) ) ;
88- } ) ;
89-
90- // Add marker for center position
91- const marker = new mapboxgl . Marker ( {
92- draggable : false ,
93- color : '#87b6a8' ,
94- } )
95- . setLngLat ( startingPosition )
96- . addTo ( map . current ) ;
9789
98- map . current . on ( 'move' , ( ) => {
99- marker . setLngLat ( map . current . getCenter ( ) ) ;
100- } ) ;
90+ map . current . on ( 'style.load' , ( ) => {
91+ map . current . setFilter ( 'photos-1940s' , [
92+ '!=' ,
93+ [ 'get' , 'photoIdentifier' ] ,
94+ photo . identifier || null ,
95+ ] ) ;
96+
97+ ( map . current as maplibregl . Map ) . addControl (
98+ new maplibregl . NavigationControl ( ) ,
99+ 'top-right'
100+ ) ;
101+ installLayers ( map . current , 'photos-1940s' , {
102+ fadeOverlays : false ,
103+ } ) ;
104+ setOverlay ( map . current , [ 'default-map' , 'bbl-label' ] ) ;
105+ } ) ;
106+
107+ map . current . on ( 'moveend' , ( ) => {
108+ const center = map . current . getCenter ( ) ;
109+ setCorrectedLngLat ( round ( center . lng , 6 ) , round ( center . lat , 6 ) ) ;
110+ } ) ;
111+
112+ // Add marker for center position
113+ const marker = new maplibregl . Marker ( {
114+ draggable : false ,
115+ color : '#87b6a8' ,
116+ } )
117+ . setLngLat ( startingPosition )
118+ . addTo ( map . current ) ;
119+
120+ map . current . on ( 'move' , ( ) => {
121+ marker . setLngLat ( map . current . getCenter ( ) ) ;
122+ } ) ;
123+ } else {
124+ // MapBox implementation
125+ map . current = new mapboxgl . Map ( {
126+ container : mapContainer . current ,
127+ style : MAPBOX_STYLE ,
128+ center : startingPosition ,
129+ maxBounds : [
130+ [ - 74.25908989999999 , 40.4773991 ] , // SW
131+ [ - 73.70027209999999 , 40.9175771 ] , // NE
132+ ] ,
133+ zoom : 17 ,
134+ hash : false ,
135+ } ) ;
136+
137+ map . current . on ( 'style.load' , ( ) => {
138+ map . current . setFilter ( 'photos-1940s' , [
139+ '!=' ,
140+ [ 'get' , 'photoIdentifier' ] ,
141+ photo . identifier || null ,
142+ ] ) ;
143+ map . current . removeLayer ( 'photos-1940s-wide-zoom' ) ;
144+
145+ ( map . current as mapboxgl . Map ) . addControl (
146+ new mapboxgl . NavigationControl ( ) ,
147+ 'top-right'
148+ ) ;
149+ installLayers ( map . current , 'photos-1940s' , {
150+ fadeOverlays : false ,
151+ } ) ;
152+ setOverlay ( map . current , [ 'default-map' , 'bbl-label' ] ) ;
153+ } ) ;
154+
155+ map . current . on ( 'moveend' , ( ) => {
156+ const center = map . current . getCenter ( ) ;
157+ setCorrectedLngLat ( round ( center . lng , 6 ) , round ( center . lat , 6 ) ) ;
158+ } ) ;
159+
160+ // Add marker for center position
161+ const marker = new mapboxgl . Marker ( {
162+ draggable : false ,
163+ color : '#87b6a8' ,
164+ } )
165+ . setLngLat ( startingPosition )
166+ . addTo ( map . current ) ;
167+
168+ map . current . on ( 'move' , ( ) => {
169+ marker . setLngLat ( map . current . getCenter ( ) ) ;
170+ } ) ;
171+ }
101172 } ;
102173
103174 const destroyMap = ( ) : void => {
0 commit comments