Skip to content

Commit 9f1d807

Browse files
authored
ZoomToLayer fix (#635)
* ZoomToLayer fix * Remove duplicate code * Replace flyTo with setView, removing animation
1 parent 268c947 commit 9f1d807

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

src/layer.js

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -321,34 +321,39 @@ export class MapLayer extends HTMLElement {
321321
bR = this.extent.bottomRight.pcrs,
322322
layerBounds = L.bounds(L.point(tL.horizontal, tL.vertical), L.point(bR.horizontal, bR.vertical)),
323323
center = map.options.crs.unproject(layerBounds.getCenter(true)),
324-
currentZoom = map.getZoom();
324+
newZoom = map.getZoom();
325325

326-
map.setView(center, currentZoom, {animate:false});
327-
let mapBounds = M.pixelToPCRSBounds(
328-
map.getPixelBounds(),
329-
map.getZoom(),
330-
map.options.projection);
326+
let maxZoom = this.extent.zoom.maxZoom, minZoom = this.extent.zoom.minZoom;
331327

332-
//fits the bounds to the map view
333-
if(mapBounds.contains(layerBounds)){
334-
while(mapBounds.contains(layerBounds) && (currentZoom + 1) <= this.extent.zoom.maxZoom){
335-
currentZoom++;
336-
map.setView(center, currentZoom, {animate:false});
337-
mapBounds = M.pixelToPCRSBounds(
338-
map.getPixelBounds(),
339-
map.getZoom(),
340-
map.options.projection);
341-
}
342-
if(currentZoom - 1 >= 0) map.flyTo(center, (currentZoom - 1));
343-
} else {
344-
while(!(mapBounds.contains(layerBounds)) && (currentZoom - 1) >= this.extent.zoom.minZoom){
345-
currentZoom--;
346-
map.setView(center, currentZoom, {animate:false});
347-
mapBounds = M.pixelToPCRSBounds(
348-
map.getPixelBounds(),
349-
map.getZoom(),
350-
map.options.projection);
351-
}
328+
let scale = map.options.crs.scale(newZoom),
329+
mapCenterTCRS = map.options.crs.transformation.transform(layerBounds.getCenter(true), scale);
330+
331+
let mapHalf = map.getSize().divideBy(2),
332+
mapTlNew = mapCenterTCRS.subtract(mapHalf).round(),
333+
mapBrNew = mapCenterTCRS.add(mapHalf).round();
334+
335+
let mapTlPCRSNew = M.pixelToPCRSPoint(mapTlNew, newZoom, map.options.projection),
336+
mapBrPCRSNew = M.pixelToPCRSPoint(mapBrNew, newZoom, map.options.projection);
337+
338+
let mapPCRS = L.bounds(mapTlPCRSNew, mapBrPCRSNew);
339+
340+
let zOffset = mapPCRS.contains(layerBounds) ? 1 : -1;
341+
342+
while((zOffset === -1 && !(mapPCRS.contains(layerBounds)) && (newZoom - 1) >= minZoom) ||
343+
(zOffset === 1 && mapPCRS.contains(layerBounds) && (newZoom + 1) <= maxZoom)) {
344+
newZoom += zOffset;
345+
346+
scale = map.options.crs.scale(newZoom);
347+
mapCenterTCRS = map.options.crs.transformation.transform(layerBounds.getCenter(true), scale);
348+
349+
mapTlNew = mapCenterTCRS.subtract(mapHalf).round();
350+
mapBrNew = mapCenterTCRS.add(mapHalf).round();
351+
mapTlPCRSNew = M.pixelToPCRSPoint(mapTlNew, newZoom, map.options.projection);
352+
mapBrPCRSNew = M.pixelToPCRSPoint(mapBrNew, newZoom, map.options.projection);
353+
354+
mapPCRS = L.bounds(mapTlPCRSNew, mapBrPCRSNew);
352355
}
356+
if(zOffset === 1 && newZoom - 1 >= 0) newZoom--;
357+
map.setView(center, newZoom, {animate: false});
353358
}
354359
}

0 commit comments

Comments
 (0)