Skip to content

Commit

Permalink
Improve handling of keeping focused area onscreen
Browse files Browse the repository at this point in the history
  • Loading branch information
quincylvania committed Aug 1, 2024
1 parent 6aff0c7 commit d861362
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,21 +307,38 @@ function buildFocusAreaGeoJson() {
}
return geoJson;
}
// check the current extent of the map, and if focused area is too far offscreen, put it back onscreen
function checkMapExtent() {
let currentBounds = map.getBounds();
let targetBounds = currentBounds.toArray();
let width = focusAreaBoundingBox[2] - focusAreaBoundingBox[0];
let height = focusAreaBoundingBox[3] - focusAreaBoundingBox[1];
let maxExtent = Math.max(width, height);
let margin = maxExtent / 4;

if (currentBounds.getNorth() < focusAreaBoundingBox[1] - margin) targetBounds[1][1] = focusAreaBoundingBox[1] + margin;
if (currentBounds.getSouth() > focusAreaBoundingBox[3] + margin) targetBounds[0][1] = focusAreaBoundingBox[3] - margin;
if (currentBounds.getEast() < focusAreaBoundingBox[0] - margin) targetBounds[1][0] = focusAreaBoundingBox[0] + margin;
if (currentBounds.getWest() > focusAreaBoundingBox[2] + margin) targetBounds[0][0] = focusAreaBoundingBox[2] - margin;
if (currentBounds.toArray().toString() !== targetBounds.toString()) {
map.fitBounds(targetBounds);
}
}
function loadFocusArea() {
focusAreaGeoJson = buildFocusAreaGeoJson();
focusAreaGeoJsonBuffered = focusAreaGeoJson?.geometry?.coordinates?.length ? turfBuffer.buffer(focusAreaGeoJson, 0.25, {units: 'kilometers'}) : focusAreaGeoJson;
focusAreaBoundingBox = bboxOfGeoJson(focusAreaGeoJson);
let maxBbox = focusAreaBoundingBox;
let fitBbox = focusAreaBoundingBox;

map.off('moveend', checkMapExtent);

if (focusAreaBoundingBox) {
let width = focusAreaBoundingBox[2] - focusAreaBoundingBox[0];
let height = focusAreaBoundingBox[3] - focusAreaBoundingBox[1];
let maxExtent = Math.max(width, height);
maxBbox = extendBbox(focusAreaBoundingBox, maxExtent);
fitBbox = extendBbox(focusAreaBoundingBox, maxExtent / 16);
let fitBbox = extendBbox(focusAreaBoundingBox, maxExtent / 16);
map.fitBounds(fitBbox);
map.on('moveend', checkMapExtent);
}
map.setMaxBounds(maxBbox);
if (fitBbox) map.fitBounds(fitBbox);
}

function focusEntity(entityInfo) {
Expand Down

0 comments on commit d861362

Please sign in to comment.