Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On update forced to previous view #137

Closed
1 of 3 tasks
zacdav opened this issue Mar 18, 2019 · 10 comments
Closed
1 of 3 tasks

On update forced to previous view #137

zacdav opened this issue Mar 18, 2019 · 10 comments
Labels
bug Something isn't working

Comments

@zacdav
Copy link
Contributor

zacdav commented Mar 18, 2019

I'm currently using mapdeck to show some data in various cities, to facilitate this I've got a dropdown which triggers a view change to the specified location with some parameters. This works great, however, when you zoom in or pan and then update the data, the view is forced back to the original view of that location.

I've provided an application which demonstrates this, if you select Melbourne, zoom in, and hit refresh, it will transition back to the view.

Is there any way to avoid this behaviour? I've tried what seems to be most permutations of the parameters for the View/Zoom and layers but can't seem to get what I'm after.

Thanks

library(shiny)
library(shinydashboard)
library(mapdeck)
library(sf)

city_info <- list("Sydney, NSW"    = list("coords" = c(151.209333, -33.863168), "zoom" = 10, "gcc" = "1GSYD"),
                  "Melbourne, VIC" = list("coords" = c(144.966892, -37.817788), "zoom" = 10, "gcc" = "2GMEL"),
                  "Brisbane, QLD"  = list("coords" = c(153.020259, -27.465076), "zoom" = 10, "gcc" = "3GBRI"),
                  "Adelaide, SA"   = list("coords" = c(138.592169, -34.925565), "zoom" = 10, "gcc" = "4GADE"),
                  "Perth, WA"      = list("coords" = c(115.859835, -31.949616), "zoom" = 10, "gcc" = "5GPER"),
                  "Hobart, TAS"    = list("coords" = c(147.330575, -42.880892), "zoom" = 11, "gcc" = "6GHOB"),
                  "Darwin, NT"     = list("coords" = c(130.935697, -12.432844), "zoom" = 11, "gcc" = "7GDAR"),
                  "Canberra, ACT"  = list("coords" = c(149.125496, -35.299607), "zoom" = 11, "gcc" = "8ACTE"))

ui <- dashboardPage(
  dashboardHeader()
  , dashboardSidebar(
    selectInput(inputId = "select_state", label = NULL,
                choices = c("Sydney, NSW",
                            "Melbourne, VIC",
                            "Brisbane, QLD",
                            "Adelaide, SA",
                            "Perth, WA",
                            "Hobart, TAS",
                            "Darwin, NT",
                            "Canberra, ACT")),
    actionButton(inputId = "test_btn", label = "Refresh")
  )
  , dashboardBody(
    mapdeckOutput(outputId = "map")
  )
)
server <- function(input, output) {
  
  ## initialise a map
  output$map <- renderMapdeck({
    mapdeck(token = Sys.getenv("MAPBOX_API_TOKEN"),
            style = mapdeck_style("dark"))
  })
  
  # change map view (fly-to)
  observeEvent(input$select_state, {
    mapdeck_update("map") %>%
      mapdeck_view(
        location = city_info[[input$select_state]]$coords,
        zoom = city_info[[input$select_state]]$zoom,
        transition = "fly",
        duration = 2000) %>%
      clear_path("roads") %>%
      add_path(data = roads[1:150, ],
               stroke_width = 5,
               id = "FQID",
               auto_highlight = TRUE,
               update_view = FALSE,
               focus_layer = FALSE)
  })
  
  # update data
  observeEvent({input$test_btn}, {
    mapdeck_update("map") %>%
      add_path(data = roads[1:150, ],
               stroke_width = 5,
               id = "FQID",
               auto_highlight = TRUE,
               update_view = FALSE,
               focus_layer = FALSE)
  })
  
}

shinyApp(ui, server)

TODO

  • revert back to 6.4.0 (or older versions) to see if the issue replicates - nope
  • try using viewState in stead of initialViewState
  • test 'fix' on google map
@zacdav zacdav changed the title On update view forced to previous Zoom On update forced to previous view Mar 18, 2019
@SymbolixAU SymbolixAU self-assigned this Mar 18, 2019
@SymbolixAU SymbolixAU added the bug Something isn't working label Mar 18, 2019
@SymbolixAU
Copy link
Collaborator

Thanks for reporting. I can't see what's triggering it to update the view so I'll need to investigate.

@SymbolixAU
Copy link
Collaborator

SymbolixAU commented Mar 19, 2019

this is the line setProps({ layers: [...window[map_id + 'layers'] ] }) in here

function md_update_layer( map_id, layer_id, layer ) {

  var elem = md_findObjectElementByKey( window[map_id + 'map'].props.layers, 'id', layer_id );
  if ( elem != -1 ) {
  	window[ map_id + 'layers'][elem] = layer;
  } else {
  	window[map_id + 'layers'].push( layer );
  }
  window[map_id + 'map'].setProps({ layers: [...window[map_id + 'layers'] ] });
}

I don't know why updating a layer causes the map to re-focus back to its starting location. This may be a deck.gl issue. I'll keep digging...

SymbolixAU pushed a commit that referenced this issue Mar 21, 2019
@dcooley
Copy link
Collaborator

dcooley commented Apr 2, 2019

same behaviour in v7.0.0-beta

@SymbolixAU SymbolixAU mentioned this issue May 12, 2019
14 tasks
SymbolixAU pushed a commit that referenced this issue May 26, 2019
SymbolixAU pushed a commit that referenced this issue May 26, 2019
@SymbolixAU
Copy link
Collaborator

I've just committed a temporary fix to the master branch. I'm not completely convinced it's fully fixed yet so will keep this issue open until I've done more testing.

@SymbolixAU
Copy link
Collaborator

A side effect of [https://github.com/SymbolixAU/mapdeck/commit/c723bacbde18c710a5555f6a1c701b358cc59ff5] is when you remove a layer it will re-focus back to the starting state of the map

library(shiny)
library(shinydashboard)
library(mapdeck)

ui <- dashboardPage(
	dashboardHeader()
	, dashboardSidebar(
		actionButton(inputId = "roads", label = "roads")
	)
	, dashboardBody(
		mapdeckOutput(outputId = "map")
	)
)

server <- function(input, output) {
	
	## initialise a map
	output$map <- renderMapdeck({
		mapdeck(
			# location = c(144.9, -37)
			# , zoom = 5
			)
	})
	
	## use an observer to add and remove layers
	observeEvent({input$roads},{
		
		if ( input$roads %% 2 == 1 ) {
			
			mapdeck_update(map_id = "map") %>%
				add_path(
					data = roads
					, layer_id = "myRoads"
					, stroke_colour = "RIGHT_LOC"
					, update_view = T
				)
		} else {
			mapdeck_update(map_id = "map") %>%
				clear_path(layer_id = "myRoads")
		}
	})
}

shinyApp(ui, server)

When adding the roads, the map focuses on the layer. When they are cleared, it goes back to starting state.

The saga continues...

SymbolixAU pushed a commit that referenced this issue May 28, 2019
@SymbolixAU
Copy link
Collaborator

When they are cleared, it goes back to starting state.

only IFF the user hasn't interacted with the map!

in the shiny example above, if you don't interact with the map between button presses, the view is returned to the previous state. However, if you do interact with the map, it stays in the last state you interacted with.

@dcooley
Copy link
Collaborator

dcooley commented Jun 12, 2019

@zacdav have you had a chance to test the latest dev version?

@zacdav
Copy link
Contributor Author

zacdav commented Jun 12, 2019

@dcooley I'll have a go tomorrow on the example I had previously.

@zacdav
Copy link
Contributor Author

zacdav commented Jun 13, 2019

@SymbolixAU @dcooley
Confirmed this is no longer misbehaving

@dcooley
Copy link
Collaborator

dcooley commented Jun 13, 2019

ok thanks. I'll close this now, but may reopen in the future if it's still not right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants