From dfc2c4d23684b4dcce3e4d8a366e58040163ed51 Mon Sep 17 00:00:00 2001 From: Mustafa Taha Şahin Date: Thu, 1 Apr 2021 00:29:15 +0300 Subject: Limit zoom levels to integers for raster maps --- modern/src/map/Map.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/modern/src/map/Map.js b/modern/src/map/Map.js index 8a43e97b..bbb1f5ce 100644 --- a/modern/src/map/Map.js +++ b/modern/src/map/Map.js @@ -35,7 +35,27 @@ const updateReadyValue = value => { readyListeners.forEach(listener => listener(value)); }; +const onIdle = function () { + const zoom = Math.round(map.getZoom()) + if (zoom !== map.getZoom()) { + map.zoomTo(zoom) + } +}; + +const isRasterMap = map => Object.values(map.getStyle().sources).some(e => e.type === 'raster'); + +const setAllowedZoomLevels = map => { + if(isRasterMap(map)) { + map.scrollZoom.setWheelZoomRate(1); + map.on('idle', onIdle); + } else { + map.scrollZoom.setWheelZoomRate(1/450); + map.off('idle', onIdle); + } +}; + const initMap = async () => { + setAllowedZoomLevels(map); const background = await loadImage('images/background.svg'); await Promise.all(deviceCategories.map(async category => { if (!map.hasImage(category)) { -- cgit v1.2.3 From d4e94f135a4092186deda57bc53ad38154cdb401 Mon Sep 17 00:00:00 2001 From: Mustafa Taha Şahin Date: Thu, 1 Apr 2021 01:48:40 +0300 Subject: refactoring --- modern/src/map/Map.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/modern/src/map/Map.js b/modern/src/map/Map.js index bbb1f5ce..4bdf2fd9 100644 --- a/modern/src/map/Map.js +++ b/modern/src/map/Map.js @@ -42,10 +42,8 @@ const onIdle = function () { } }; -const isRasterMap = map => Object.values(map.getStyle().sources).some(e => e.type === 'raster'); - const setAllowedZoomLevels = map => { - if(isRasterMap(map)) { + if (Object.values(map.getStyle().sources).some(e => e.type === 'raster')) { map.scrollZoom.setWheelZoomRate(1); map.on('idle', onIdle); } else { -- cgit v1.2.3 From 203df66558a9d92dce11d6121e576886d29c2e77 Mon Sep 17 00:00:00 2001 From: Mustafa Taha Şahin Date: Fri, 2 Apr 2021 01:14:03 +0300 Subject: more refactoring --- modern/src/map/Map.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/modern/src/map/Map.js b/modern/src/map/Map.js index 4bdf2fd9..7f789734 100644 --- a/modern/src/map/Map.js +++ b/modern/src/map/Map.js @@ -42,18 +42,7 @@ const onIdle = function () { } }; -const setAllowedZoomLevels = map => { - if (Object.values(map.getStyle().sources).some(e => e.type === 'raster')) { - map.scrollZoom.setWheelZoomRate(1); - map.on('idle', onIdle); - } else { - map.scrollZoom.setWheelZoomRate(1/450); - map.off('idle', onIdle); - } -}; - const initMap = async () => { - setAllowedZoomLevels(map); const background = await loadImage('images/background.svg'); await Promise.all(deviceCategories.map(async category => { if (!map.hasImage(category)) { @@ -111,6 +100,17 @@ const Map = ({ children }) => { }; }, []); + useEffect(() => { + if (mapReady && Object.values(map.getStyle().sources).some(e => e.type === 'raster')) { + map.scrollZoom.setWheelZoomRate(1); + map.on('idle', onIdle); + } + return () => { + map.scrollZoom.setWheelZoomRate(1/450); + map.off('idle', onIdle); + } + }, [mapReady]); + useLayoutEffect(() => { const currentEl = containerEl.current; currentEl.appendChild(element); -- cgit v1.2.3 From 56567e683734c1f990fb59f193c2d96df706b6b0 Mon Sep 17 00:00:00 2001 From: Mustafa Taha Şahin Date: Fri, 2 Apr 2021 02:40:07 +0300 Subject: another refactoring --- modern/src/map/Map.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/modern/src/map/Map.js b/modern/src/map/Map.js index 7f789734..d83c297d 100644 --- a/modern/src/map/Map.js +++ b/modern/src/map/Map.js @@ -35,13 +35,6 @@ const updateReadyValue = value => { readyListeners.forEach(listener => listener(value)); }; -const onIdle = function () { - const zoom = Math.round(map.getZoom()) - if (zoom !== map.getZoom()) { - map.zoomTo(zoom) - } -}; - const initMap = async () => { const background = await loadImage('images/background.svg'); await Promise.all(deviceCategories.map(async category => { @@ -102,12 +95,18 @@ const Map = ({ children }) => { useEffect(() => { if (mapReady && Object.values(map.getStyle().sources).some(e => e.type === 'raster')) { - map.scrollZoom.setWheelZoomRate(1); - map.on('idle', onIdle); + const onIdle = function () { + const zoom = Math.round(map.getZoom()); + if (zoom !== map.getZoom()) { + map.zoomTo(zoom); + } + }; + map.scrollZoom.setWheelZoomRate(1); + map.on('idle', onIdle); + return () => { + map.scrollZoom.setWheelZoomRate(1/450); + map.off('idle', onIdle); } - return () => { - map.scrollZoom.setWheelZoomRate(1/450); - map.off('idle', onIdle); } }, [mapReady]); -- cgit v1.2.3