Skip to content

Commit

Permalink
observations map: fitBounds to selected observations
Browse files Browse the repository at this point in the history
  • Loading branch information
tsubik committed Mar 29, 2024
1 parent 3b5fe97 commit b75f363
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion components/map/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,13 @@ class Map extends Component {
...options
});

// TODO: not sure why fitBounds maxZoom is not working, so fixing that manually
const useMaxZoom = options.maxZoom && zoom > options.maxZoom;
const newViewport = {
...this.state.viewport,
longitude,
latitude,
zoom,
zoom: useMaxZoom ? options.maxZoom : zoom,
transitionDuration
};

Expand Down
21 changes: 21 additions & 0 deletions pages/observations.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { connect } from 'react-redux';
import { withRouter } from 'next/router';
import { injectIntl } from 'react-intl';

import getBBox from '@turf/bbox';

import modal from 'services/modal';

import { transformRequest } from 'utils/map';
Expand Down Expand Up @@ -98,6 +100,23 @@ class ObservationsPage extends React.Component {
if (!isEqual(this.props.router.query, prevProps.router.query)) {
this.props.getObservationsUrl(this.props.router);
}

// when the data is loaded fitBounds to selected observations
if (!isEqual(this.props.observations.data, prevProps.observations.data)) {
const obsLayer = this.props.getObservationsLayers.find((l) => l.id === 'observations');
if (obsLayer) {
const bbox = getBBox(obsLayer.source.data);
this.setState({
bounds: {
bbox,
options: {
padding: 50,
maxZoom: 6
},
}
});
}
}
}

getPageSize() {
Expand Down Expand Up @@ -418,6 +437,8 @@ class ObservationsPage extends React.Component {
// viewport
viewport={observations.map}
onViewportChange={this.onViewportChange}
// Bounds
bounds={this.state.bounds}
// Interaction
interactiveLayerIds={interactiveLayerIds}
onClick={this.onClick}
Expand Down

0 comments on commit b75f363

Please sign in to comment.