From cae6ffcb8113da16a32c5ace38ebcf7ad75e0e60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hern=C3=A1n=20Mej=C3=ADa?= Date: Mon, 20 May 2024 18:48:58 -0400 Subject: [PATCH] Clean code and support viewState access with viewId --- inst/htmlwidgets/mapdeck.js | 2 +- inst/htmlwidgets/mapdeck_location.js | 36 ++++++++++------------------ 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/inst/htmlwidgets/mapdeck.js b/inst/htmlwidgets/mapdeck.js index fa27f469..d00a6b26 100644 --- a/inst/htmlwidgets/mapdeck.js +++ b/inst/htmlwidgets/mapdeck.js @@ -101,7 +101,7 @@ HTMLWidgets.widget({ mapboxApiAccessToken: x.access_token, container: el.id, mapStyle: x.style, - initialViewState: window[el.id + 'INITIAL_VIEW_STATE'], + viewState: window[el.id + 'INITIAL_VIEW_STATE'], layers: [], controller: true, //onLayerHover: setTooltip diff --git a/inst/htmlwidgets/mapdeck_location.js b/inst/htmlwidgets/mapdeck_location.js index 1ea1d92a..8fb28cf9 100644 --- a/inst/htmlwidgets/mapdeck_location.js +++ b/inst/htmlwidgets/mapdeck_location.js @@ -14,30 +14,18 @@ function md_change_location( map_id, map_type, location, zoom, pitch, bearing, d window[map_id + 'map'].setZoom( zoom ); } else { - console.log( window[ map_id + 'map' ].viewManager.viewState.longitude ); - - if ( window[ map_id + 'map'].viewManager.viewState["default-view"] !== undefined ) { - currentLon = (location === null || location.length == 0) ? window[ map_id + 'map'].viewManager.viewState["default-view"].longitude : location[0]; - currentLat = (location === null || location.length == 0) ? window[ map_id + 'map'].viewManager.viewState["default-view"].latitude : location[1]; - currentPitch = pitch === null ? window[ map_id + 'map'].viewManager.viewState["default-view"].pitch : pitch; - currentBearing = bearing === null ? window[ map_id + 'map' ].viewManager.viewState["default-view"].bearing : bearing; - currentZoom = zoom === null ? window[ map_id + 'map'].viewManager.viewState["default-view"].zoom : zoom; - currentMaxZoom = window[ map_id + 'map'].viewManager.viewState["default-view"].maxZoom; - currentMinZoom = window[ map_id + 'map'].viewManager.viewState["default-view"].minZoom; - currentMaxPitch = window[ map_id + 'map'].viewManager.viewState["default-view"].maxPitch; - currentMinPitch = window[ map_id + 'map'].viewManager.viewState["default-view"].minPitch; - } else { - currentViewState = window[ map_id + 'map'].viewManager.viewState - currentLon = (location === null || location.length == 0) ? currentViewState.longitude : location[0]; - currentLat = (location === null || location.length == 0) ? currentViewState.latitude : location[1]; - currentPitch = pitch === null ? currentViewState.pitch : pitch; - currentBearing = bearing === null ? currentViewState.bearing : bearing; - currentZoom = zoom === null ? currentViewState.zoom : zoom; - currentMaxZoom = currentViewState.maxZoom; - currentMinZoom = currentViewState.minZoom; - currentMaxPitch = currentViewState.maxPitch; - currentMinPitch = currentViewState.minPitch; - } + console.log( window[ map_id + 'map' ].viewManager.getViewState(map_id).longitude ); + + currentViewState = window[ map_id + 'map'].viewManager.getViewState(map_id) + currentLon = (location === null || location.length == 0) ? currentViewState.longitude : location[0]; + currentLat = (location === null || location.length == 0) ? currentViewState.latitude : location[1]; + currentPitch = pitch === null ? currentViewState.pitch : pitch; + currentBearing = bearing === null ? currentViewState.bearing : bearing; + currentZoom = zoom === null ? currentViewState.zoom : zoom; + currentMaxZoom = currentViewState.maxZoom; + currentMinZoom = currentViewState.minZoom; + currentMaxPitch = currentViewState.maxPitch; + currentMinPitch = currentViewState.minPitch; console.log( currentLon );