@@ -41,6 +41,7 @@ import {Link} from 'react-router-dom';
41
41
import * as ROUTES from '../../../constants/routes' ;
42
42
import { createCenterControl } from '../map/center-control' ;
43
43
import { Geolocation } from '@capacitor/geolocation' ;
44
+ import { delay } from 'lodash' ;
44
45
45
46
interface OverviewMapProps {
46
47
uiSpec : ProjectUIModel ;
@@ -140,7 +141,7 @@ export const OverviewMap = (props: OverviewMapProps) => {
140
141
const mapRef = useRef < Map | undefined > ( ) ;
141
142
mapRef . current = map ;
142
143
143
- const { data : map_center } = useQuery ( {
144
+ const { data : map_center , isLoading : loadingLocation } = useQuery ( {
144
145
queryKey : [ 'current_location' ] ,
145
146
queryFn : async ( ) : Promise < [ number , number ] > => {
146
147
const position = await Geolocation . getCurrentPosition ( ) ;
@@ -152,42 +153,31 @@ export const OverviewMap = (props: OverviewMapProps) => {
152
153
/**
153
154
* Create the OpenLayers map element
154
155
*/
155
- const createMap = useCallback (
156
- async ( element : HTMLElement ) : Promise < Map > => {
157
- const center = transform ( map_center , 'EPSG:4326' , defaultMapProjection ) ;
158
-
159
- const tileLayer = new TileLayer ( { source : new OSM ( ) } ) ;
160
- const view = new View ( {
161
- projection : defaultMapProjection ,
162
- center : center ,
163
- zoom : 12 ,
164
- } ) ;
165
-
166
- const theMap = new Map ( {
167
- target : element ,
168
- layers : [ tileLayer ] ,
169
- view : view ,
170
- controls : [ new Zoom ( ) ] ,
171
- } ) ;
172
-
173
- theMap . addControl ( createCenterControl ( theMap . getView ( ) , center ) ) ;
156
+ const createMap = useCallback ( async ( element : HTMLElement ) : Promise < Map > => {
157
+ const tileLayer = new TileLayer ( { source : new OSM ( ) } ) ;
158
+ const view = new View ( {
159
+ projection : defaultMapProjection ,
160
+ } ) ;
174
161
175
- theMap . getView ( ) . setCenter ( center ) ;
162
+ const theMap = new Map ( {
163
+ target : element ,
164
+ layers : [ tileLayer ] ,
165
+ view : view ,
166
+ controls : [ new Zoom ( ) ] ,
167
+ } ) ;
176
168
177
- theMap . on ( 'click' , evt => {
178
- const feature = theMap . forEachFeatureAtPixel ( evt . pixel , feature => {
179
- return feature . getProperties ( ) ;
180
- } ) ;
181
- if ( ! feature ) {
182
- return ;
183
- }
184
- setSelectedFeature ( feature as FeatureProps ) ;
169
+ theMap . on ( 'click' , evt => {
170
+ const feature = theMap . forEachFeatureAtPixel ( evt . pixel , feature => {
171
+ return feature . getProperties ( ) ;
185
172
} ) ;
173
+ if ( ! feature ) {
174
+ return ;
175
+ }
176
+ setSelectedFeature ( feature as FeatureProps ) ;
177
+ } ) ;
186
178
187
- return theMap ;
188
- } ,
189
- [ map_center ]
190
- ) ;
179
+ return theMap ;
180
+ } , [ ] ) ;
191
181
192
182
/**
193
183
* Add a marker to the map at the current location
@@ -269,20 +259,35 @@ export const OverviewMap = (props: OverviewMapProps) => {
269
259
// don't fit if the extent is infinite because it crashes
270
260
if ( ! extent . includes ( Infinity ) ) {
271
261
map . getView ( ) . fit ( extent , { padding : [ 100 , 100 , 100 , 100 ] , maxZoom : 12 } ) ;
262
+ const computedCenter = map . getView ( ) . getCenter ( ) ;
263
+ console . log ( 'computed center' , computedCenter ) ;
272
264
}
273
265
}
274
266
275
267
map . addLayer ( layer ) ;
276
268
} ;
277
269
270
+ // when we have a location and a map, add the 'here' marker to the map
271
+ if ( ! loadingLocation && map ) {
272
+ addCurrentLocationMarker ( map ) ;
273
+ if ( map_center ) {
274
+ const center = transform ( map_center , 'EPSG:4326' , defaultMapProjection ) ;
275
+ // add the 'here' button to go to the current location
276
+ map . addControl ( createCenterControl ( map . getView ( ) , center ) ) ;
277
+ }
278
+ }
279
+
280
+ // when we have features, add them to the map
281
+ if ( ! loadingFeatures && map ) {
282
+ addFeaturesToMap ( map ) ;
283
+ }
284
+
278
285
// callback to add the map to the DOM
279
286
const refCallback = ( element : HTMLElement | null ) => {
280
287
if ( element ) {
281
288
if ( ! map ) {
282
289
// create map
283
290
createMap ( element ) . then ( ( theMap : Map ) => {
284
- addFeaturesToMap ( theMap ) ;
285
- addCurrentLocationMarker ( theMap ) ;
286
291
setMap ( theMap ) ;
287
292
} ) ;
288
293
} else {
0 commit comments