@@ -16,33 +16,31 @@ import { getDevicePreferences } from '#app/core/preferences.ts';
1616import type { Map } from ' maplibre-gl' ;
1717import type { StyleSpecification } from ' maplibre-gl' ;
1818import type { PolygonPoint } from ' #app/utils/tile-preloader.ts' ;
19+ import type { MapState } from ' ./-utils/map-state' ;
20+ import { excludeNull , or } from ' #app/utils/comparison.ts' ;
1921
2022interface MapLibreFullscreenMapSignature {
2123 Args: {
2224 /**
23- * Location ID for caching
25+ * The MapState instance to persist map state
26+ * like center and zoom level
2427 */
25- locationId : string ;
28+ mapState : MapState ;
2629
2730 /**
2831 * Location name for display
2932 */
3033 locationName: string ;
3134
3235 /**
33- * Center latitude
36+ * Location latitude
3437 */
35- lat: number ;
38+ lat? : number ;
3639
3740 /**
38- * Center longitude
41+ * Location longitude
3942 */
40- lng: number ;
41-
42- /**
43- * Initial zoom level (default: 14)
44- */
45- zoom? : number ;
43+ lng? : number ;
4644
4745 /**
4846 * Map style (default: OpenStreetMap style)
@@ -62,7 +60,6 @@ export default class MapLibreFullscreenMap extends Component<MapLibreFullscreenM
6260 preferences = getDevicePreferences ();
6361
6462 map: Map | null = null ;
65- currentZoom: number = this .args .zoom ?? 14 ;
6663 locationWatchId: number | null = null ;
6764
6865 @tracked
@@ -127,7 +124,6 @@ export default class MapLibreFullscreenMap extends Component<MapLibreFullscreenM
127124 // Watch position with high accuracy
128125 this .locationWatchId = navigator .geolocation .watchPosition (
129126 (position ) => {
130- console .log (' Received location update:' , position );
131127 const { latitude, longitude } = position .coords ;
132128
133129 this .userLocation = { lat: latitude , lng: longitude };
@@ -152,9 +148,10 @@ export default class MapLibreFullscreenMap extends Component<MapLibreFullscreenM
152148 this .userLocation = null ;
153149 };
154150
155- storeMapReference = (context : { map: Map } | null ) => {
151+ storeMapReference = (context : Map | null ) => {
152+ // console.log('Storing map reference in fullscreen map component', context, this.map);
156153 if (context && ! this .map ) {
157- this .map = context . map ;
154+ this .map = context ;
158155 }
159156 }
160157
@@ -183,6 +180,22 @@ export default class MapLibreFullscreenMap extends Component<MapLibreFullscreenM
183180 this .polygon = null ;
184181 };
185182
183+ updateLocation = () => {
184+ const center = this .map ?.getCenter ();
185+ if (center ) {
186+ this .args .mapState .lng = center .lng // Number(center.lng.toFixed(5));
187+ this .args .mapState .lat = center .lat // Number(center.lat.toFixed(5));
188+ }
189+ }
190+
191+ updateZoom = () => {
192+ const zoom = this .map ?.getZoom ();
193+ console .log (' Map zoom updated:' , zoom );
194+ if (zoom ) {
195+ this .args .mapState .zoom = zoom ;
196+ }
197+ }
198+
186199 <template >
187200 {{#in-element this . portals.takeover }}
188201 {{! template-lint-disable no-inline-styles }}
@@ -201,10 +214,10 @@ export default class MapLibreFullscreenMap extends Component<MapLibreFullscreenM
201214 {{! Download button - bottom right }}
202215 <div class =" fullscreen-map-download" >
203216 <MapDownloadButton
204- @ locationId ={{@ locationId }}
217+ @ locationId ={{@ mapState.id }}
205218 @ locationName ={{@ locationName }}
206- @ lat ={{@ lat }}
207- @ lng ={{@ lng }}
219+ @ lat ={{@ mapState. lat }}
220+ @ lng ={{@ mapState. lng }}
208221 @ getMap ={{this .getMap }}
209222 @ polygon ={{this .polygon }}
210223 @ onStartPolygonSelection ={{this .startPolygonSelection }}
@@ -225,34 +238,34 @@ export default class MapLibreFullscreenMap extends Component<MapLibreFullscreenM
225238
226239 <MapLibreBoundary >
227240 <MapLibreMap
228- @ lat ={{@ lat }}
229- @ lng ={{@ lng }}
230- @ zoom ={{this .currentZoom }}
241+ @ lat ={{or @ mapState.lat @ mapState.defaultLat}}
242+ @ lng ={{or @ mapState.lng @ mapState.defaultLng}}
243+ @ zoom ={{or @ mapState.zoom @ mapState.defaultZoom}}
244+ @ onMoveEnd ={{this .updateLocation }}
245+ @ onZoomEnd ={{this .updateZoom }}
231246 @ style ={{@ style }}
232247 as | context |
233248 >
234- {{#if context }}
235- {{this .storeMapReference context }}
249+ {{this .storeMapReference context }}
250+ <MapLibreMarker
251+ @ context ={{context }}
252+ @ lat ={{excludeNull ( or @ mapState.defaultLat @ lat) }}
253+ @ lng ={{excludeNull ( or @ mapState.defaultLng @ lng) }}
254+ @ title ={{@ locationName }}
255+ />
256+
257+ {{! User location marker }}
258+ {{#if this . userLocation }}
259+ {{#if this . userLocationElement }}
236260 <MapLibreMarker
237261 @ context ={{context }}
238- @ lat ={{@ lat }}
239- @ lng ={{@ lng }}
240- @ title ={{@ locationName }}
262+ @ lat ={{this .userLocation.lat }}
263+ @ lng ={{this .userLocation.lng }}
264+ @ title =" Your Location"
265+ @ element ={{this .userLocationElement }}
241266 />
242-
243- {{! User location marker }}
244- {{#if this . userLocation }}
245- {{#if this . userLocationElement }}
246- <MapLibreMarker
247- @ context ={{context }}
248- @ lat ={{this .userLocation.lat }}
249- @ lng ={{this .userLocation.lng }}
250- @ title =" Your Location"
251- @ element ={{this .userLocationElement }}
252- />
253- {{/if }}
254- {{/if }}
255267 {{/if }}
268+ {{/if }}
256269 </MapLibreMap >
257270 </MapLibreBoundary >
258271 </div >
0 commit comments