Skip to content

Commit

Permalink
fix: Fit to bounds if not custom bounds selected
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui committed Oct 25, 2023
1 parent 270bf4a commit 72af7cc
Showing 1 changed file with 45 additions and 22 deletions.
67 changes: 45 additions & 22 deletions src/sharedData/visualization/components/Visualization.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,35 @@ import { getLayerImage } from 'sharedData/visualization/visualizationMarkers';

import { sheetToGeoJSON } from '../visualizationUtils';

const getSourceBounds = async (map, sourceId) => {
const source = map.getSource(sourceId);
const loaded = source.loaded();
if (!source) {
return;
}

const loadedSource = loaded
? source
: await new Promise(resolve => {
map.on('sourcedata', () => {
const source = map.getSource('visualization');
if (source.loaded()) {
resolve(source);
}
});
});

if (loadedSource.bounds) {
return new mapboxgl.LngLatBounds(loadedSource.bounds);
}

const calculatedBbox = bbox(loadedSource._data);
return new mapboxgl.LngLatBounds(
[calculatedBbox[0], calculatedBbox[1]],
[calculatedBbox[2], calculatedBbox[3]]
);
};

const PopupContent = props => {
const { data } = props;
const fields = JSON.parse(data.fields);
Expand Down Expand Up @@ -188,7 +217,7 @@ const MapboxLayer = props => {
if (!map) {
return;
}
const visualizationConfigBounds = (function () {
const visualizationConfigBounds = (async function () {
const viewportBounds = visualizationConfig?.viewportConfig?.bounds;
if (!viewportBounds) {
return;
Expand All @@ -203,29 +232,23 @@ const MapboxLayer = props => {
return new mapboxgl.LngLatBounds(sw, ne);
})();

const geoJsonBounds = (function () {
const source = map.getSource('visualization');
if (!source || !source._data) {
return;
}
const calculatedBbox = bbox(map.getSource('visualization')._data);
return new mapboxgl.LngLatBounds(
[calculatedBbox[0], calculatedBbox[1]],
[calculatedBbox[2], calculatedBbox[3]]
);
})();
const sourceBounds = getSourceBounds(map, 'visualization');

const bounds =
useConfigBounds && visualizationConfigBounds
? visualizationConfigBounds
: geoJsonBounds;
Promise.all([visualizationConfigBounds, sourceBounds]).then(
([visualizationConfigBounds, sourceBounds]) => {
const bounds =
useConfigBounds && visualizationConfigBounds
? visualizationConfigBounds
: sourceBounds;

if (bounds && !bounds.isEmpty()) {
map.fitBounds(bounds, {
padding: 50,
animate: false,
});
}
if (bounds && !bounds.isEmpty()) {
map.fitBounds(bounds, {
padding: 50,
animate: false,
});
}
}
);
}, [map, visualizationConfig?.viewportConfig?.bounds, useConfigBounds]);

const layer = useMemo(() => {
Expand Down

0 comments on commit 72af7cc

Please sign in to comment.