Skip to content

Commit 42bc167

Browse files
committed
Try to handle race condition in map creation
Signed-off-by: Steve Cassidy <steve.cassidy@mq.edu.au>
1 parent 2ed3f77 commit 42bc167

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

app/src/gui/components/notebook/overview_map.tsx

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {Link} from 'react-router-dom';
4141
import * as ROUTES from '../../../constants/routes';
4242
import {createCenterControl} from '../map/center-control';
4343
import {Geolocation} from '@capacitor/geolocation';
44+
import {delay} from 'lodash';
4445

4546
interface OverviewMapProps {
4647
uiSpec: ProjectUIModel;
@@ -140,7 +141,7 @@ export const OverviewMap = (props: OverviewMapProps) => {
140141
const mapRef = useRef<Map | undefined>();
141142
mapRef.current = map;
142143

143-
const {data: map_center} = useQuery({
144+
const {data: map_center, isLoading: loadingLocation} = useQuery({
144145
queryKey: ['current_location'],
145146
queryFn: async (): Promise<[number, number]> => {
146147
const position = await Geolocation.getCurrentPosition();
@@ -152,42 +153,31 @@ export const OverviewMap = (props: OverviewMapProps) => {
152153
/**
153154
* Create the OpenLayers map element
154155
*/
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+
});
174161

175-
theMap.getView().setCenter(center);
162+
const theMap = new Map({
163+
target: element,
164+
layers: [tileLayer],
165+
view: view,
166+
controls: [new Zoom()],
167+
});
176168

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();
185172
});
173+
if (!feature) {
174+
return;
175+
}
176+
setSelectedFeature(feature as FeatureProps);
177+
});
186178

187-
return theMap;
188-
},
189-
[map_center]
190-
);
179+
return theMap;
180+
}, []);
191181

192182
/**
193183
* Add a marker to the map at the current location
@@ -269,20 +259,35 @@ export const OverviewMap = (props: OverviewMapProps) => {
269259
// don't fit if the extent is infinite because it crashes
270260
if (!extent.includes(Infinity)) {
271261
map.getView().fit(extent, {padding: [100, 100, 100, 100], maxZoom: 12});
262+
const computedCenter = map.getView().getCenter();
263+
console.log('computed center', computedCenter);
272264
}
273265
}
274266

275267
map.addLayer(layer);
276268
};
277269

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+
278285
// callback to add the map to the DOM
279286
const refCallback = (element: HTMLElement | null) => {
280287
if (element) {
281288
if (!map) {
282289
// create map
283290
createMap(element).then((theMap: Map) => {
284-
addFeaturesToMap(theMap);
285-
addCurrentLocationMarker(theMap);
286291
setMap(theMap);
287292
});
288293
} else {

0 commit comments

Comments
 (0)