Skip to content

Commit

Permalink
tracking view state change #382
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Jul 2, 2024
1 parent a396a2a commit 3d2d5ab
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: mapdeck
Type: Package
Title: Interactive Maps Using 'Mapbox GL JS' and 'Deck.gl'
Version: 0.3.5
Date: 2024-01-29
Version: 0.3.6
Date: 2024-07-02
Authors@R: c(
person("David", "Cooley", ,"[email protected]", role = c("aut", "cre"))
)
Expand Down
3 changes: 2 additions & 1 deletion inst/htmlwidgets/lib/view_state/view_state.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
function add_view_state( map_id, viewState ) {
'use strict';
Expand Down Expand Up @@ -26,7 +27,6 @@ function add_view_state( map_id, viewState ) {
}
}

function md_placeViewState( map_id, object ) {
var mapbox_ctrl = document.getElementById( "mapViewStateContainer"+map_id);
Expand All @@ -44,3 +44,4 @@ function clear_view_state( map_id ) {
}
*/
34 changes: 18 additions & 16 deletions inst/htmlwidgets/mapdeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ HTMLWidgets.widget({
}

// INITIAL VIEW
window[el.id + 'INITIAL_VIEW_STATE'] = {
const initialViewState = {
longitude: x.location[0],
latitude: x.location[1],
zoom: x.zoom,
Expand All @@ -73,40 +73,42 @@ HTMLWidgets.widget({
minPitch: x.min_pitch
};

window[el.id + 'viewState'] = initialViewState;

const mapView = new deck.MapView({
id: el.id,
repeat: x.repeat_view
})

if( x.access_token === null ) {
deckgl = new deck.DeckGL({
views: [ new deck.MapView({
id: el.id,
repeat: x.repeat_view,
// width: width,
// height: height
}) ],
views: [mapView],
map: false,
container: el.id,
initialViewState: window[el.id + 'INITIAL_VIEW_STATE'],
initialViewState: initialViewState,
layers: [],
controller: true
//onLayerHover: setTooltip
});
window[el.id + 'map'] = deckgl;
} else {
deckgl = new deck.DeckGL({
views: [ new deck.MapView({
id: el.id,
repeat: x.repeat_view,
// width: width,
// height: height
})
],
views: [mapView],
mapboxApiAccessToken: x.access_token,
container: el.id,
mapStyle: x.style,
initialViewState: window[el.id + 'INITIAL_VIEW_STATE'],
initialViewState: initialViewState,
layers: [],
controller: true,
//onLayerHover: setTooltip
onViewStateChange: ({viewId, viewState, interactionState}) => {

//console.log("onViewStateChange");
//console.log(viewState);

// issue 382
window[el.id + 'viewState'] = viewState;

if (!HTMLWidgets.shinyMode && !x.show_view_state ) { return; }
// as per:
// https://github.com/uber/deck.gl/issues/3344
Expand Down
19 changes: 18 additions & 1 deletion inst/htmlwidgets/mapdeck_location.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,25 @@ function md_change_location( map_id, map_type, location, zoom, pitch, bearing, d

currentViewState = window[ map_id + 'map'].viewState["default-view"];
} else {
currentViewState = window[ map_id + 'map'].viewState;
//currentViewState = window[ map_id + 'map'].viewState;
currentViewState = window[ map_id + 'viewState'];
}
/*
console.log("default-view");
console.log(window[ map_id + 'map'].viewState["default-view"]);
console.log("viewState");
console.log(window[ map_id + 'map'].viewState);
console.log(window[ map_id + 'viewState']);
console.log("viewManager");
console.log(window[ map_id + 'map'].viewManager);
console.log("viewManager.viewState");
console.log(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];
Expand Down

0 comments on commit 3d2d5ab

Please sign in to comment.