SymbolixAU / googleway

R Package for accessing and plotting Google Maps
http://symbolixau.github.io/googleway/
Other
234 stars 46 forks source link

Street view not updating with map using split view #190

Open rtaylor741 opened 5 years ago

rtaylor741 commented 5 years ago

Hi,

I'm trying to build an app where inputting a new address moves to that address in google maps and shows the map and street view using your split_view functionality. When a new address is chosen, the map changes, but the location of the street view point doesn't. I've put some basic code below that shows my problem. Is there a way to input a location for street view? Ideally I would use a postcode centre lat/lon for when the postcode is selected and then the address lat/lon for when the address is selected.

Thanks for your help!

library(shiny)
library(DT)
library(googleway)

ui <- fluidPage(
  fluidRow(
    box(width=12,
        column(width=3,
               selectInput(inputId = "postcodes", label = NULL, choices=c('','SW17 8PB','E1 3DY'), multiple=FALSE, selected='')
        ),
        column(width=3,
               selectInput(inputId = "addresses", label = NULL, choices=NULL, multiple=FALSE)
               )
        ) #box
    ), #row
  div(
    fluidRow(
      box(width = 6,
          google_mapOutput(outputId = "map")
          ),
      box(width = 6,
          google_mapOutput(outputId = "pano")
          )
      ) #row
    ) #div
  )

server <- function(input, output, session) {

  temp_data <- data.frame('postcode'=c('E1 3DY','E1 3DY','E1 3DY','SW17 8PB','SW17 8PB','SW17 8PB'),
                     'building_number'=c('1','2','3','21','12','17'),'lat'=c(51.5170971,51.5171136,51.5171037,51.4310593,51.4309847,51.4311056),
                     'lon'=c(-0.0544549,-0.0543677,-0.0543105,-0.162327,-0.1627329,-0.1624115))
  #Base map

  set_key("xxx")

  output$map <- renderGoogle_map({
    google_map(location = c(51.5074,0.1278),zoom = 16, split_view = "pano"
               )
  })

  #-------------------------------------------------------------------------------------------------------
  #Search Postcode button
  observeEvent(input$postcodes, {

    pc_data <- subset(temp_data,postcode==input$postcodes)

    #Create Address list
    address_list <- c('', as.character(pc_data$building_number)) #add empty address for starting value

    updateSelectInput(session,"addresses",choices = address_list, selected = "")

      google_map_update("map",data = pc_data) %>%
        clear_bounds() %>%
        clear_markers() %>%
        add_markers(lat = "lat", lon = "lon", update_map_view=TRUE, mouse_over = "building_number")

  })

  #-------------------------------------------------------------------------------------------------------
  #What to do when address is selected
  observeEvent(input$addresses, {

    if(input$addresses != "") {

      address_row <- subset(temp_data,postcode==input$postcodes & building_number==input$addresses) #select address row

      google_map_update("map",data = address_row) %>%
        clear_bounds() %>%
        clear_markers() %>%
        add_markers(lat = "lat", lon = "lon", update_map_view=FALSE, mouse_over = "building_number")

    }

  })
  #-------------------------------------------------------------------------------------------------------

} #server

#This allows the ui and server code to be in one file
shinyApp(ui, server)  
SymbolixAU commented 5 years ago

I can't run your code, the UI errors with

Error in box(width = 12, column(width = 3, selectInput(inputId = "postcodes",  : 
  plot.new has not been called yet
rtaylor741 commented 5 years ago

Hi, the code runs fine for me. Looking at that error online it seems to be something do with R itself. A lot of people saying that the error only appears when they run code in chunks instead of the whole script?

https://stackoverflow.com/questions/40938561/plot-new-has-not-been-called-yet-error-in-rmarkdown-rstudio-1-0-44

SymbolixAU commented 5 years ago

ah - I spotted the error - box is from shinydashboard, so you need that library loaded.

Can I clarify what your issue is:

When you enter a postcode you want both the map and the streetview to update their locations?

rtaylor741 commented 5 years ago

ah sorry, yes that could have been loaded already from a separate script.

Yes, that is exactly right. I know you can drag the peg man onto the map and the streetview will update, but i would like to be able to have it update automatically.

Thanks for looking into it.

SymbolixAU commented 5 years ago

Ok I understand; that's a good question and would be a useful feature, but I'll have to look into it tomorrow (Australian time)

SymbolixAU commented 5 years ago

I think as an interim solution you could not use the google_map_update and just re-draw the map each time. It will be a bit jerky but might get you a solution.


Notes for me

will need to grab a reference to map.setStreetView() and implement a update_pano() function.

rtaylor741 commented 5 years ago

No problem, that would be great.

I did try redrawing the map again each time, but I was having issues with that too. Not sure if related to your package or not, but redrawing a map with the same ID didn't seem to work and I had to rename it to "map2" and update the UI to "map2" as well even though I've done this kind of thing loads of times for data frames etc. This worked, but then every time you selected a new address it would need a new ID and it was causing me problems. As I say, not sure that's related to your package.

Thanks again.

SymbolixAU commented 5 years ago

I've updated the github version with an update_pano() function which I think is along the right lines. You use it like

google_map_update( "map", data = pc_data ) %>%
  clear_bounds() %>%
  clear_markers() %>%
  add_markers(lat = "lat", lon = "lon", update_map_view = TRUE, mouse_over = "building_number") %>%
  update_pano( pano = "pano", lat = pc_data$lat[1], lon = pc_data$lon[1])

(In this example the lat & lon 's are incorrect, but you should get the idea.

rtaylor741 commented 5 years ago

Thanks for getting this done so quickly!

I can't quite get it to work properly though. I've added columns called latc and lonc as a random place to see if it will move the streetview to there, but it doesn't seem to work. It doesn't update the streetview when you first select a postcode, but then it does every time you select from then on, but with one of the address points. Code below that hopefully shows what I mean better.

library(shiny)
library(shinydashboard)
library(DT)
library(googleway)

ui <- fluidPage(
  fluidRow(
    box(width=12,
        column(width=3,
               selectInput(inputId = "postcodes", label = NULL, choices=c('','SW17 8PB','E1 3DY'), multiple=FALSE, selected='')
        ),
        column(width=3,
               selectInput(inputId = "addresses", label = NULL, choices=NULL, multiple=FALSE)
        )
    ) #box
  ), #row
  div(
    fluidRow(
      box(width = 6,
          google_mapOutput(outputId = "map")
      ),
      box(width = 6,
          google_mapOutput(outputId = "pano")
      )
    ) #row
  ) #div
)

server <- function(input, output, session) {

  temp_data <- data.frame('postcode'=c('E1 3DY','E1 3DY','E1 3DY','SW17 8PB','SW17 8PB','SW17 8PB'),
                          'building_number'=c('1','2','3','21','12','17'),'lat'=c(51.5170971,51.5171136,51.5171037,51.4310593,51.4309847,51.4311056),
                          'lon'=c(-0.0544549,-0.0543677,-0.0543105,-0.162327,-0.1627329,-0.1624115),
                          'latc'=c(52.6287351,52.6287351,52.6287351,52.6287351,52.6287351,52.6287351),
                          'lonc'=c(1.3781001,1.3781001,1.3781001,-1.3781001,-1.3781001,-1.3781001)
                          )
  #Base map

  set_key("xxx")

  output$map <- renderGoogle_map({
    google_map(location = c(51.5074,0.1278),zoom = 16, split_view = "pano"
    )
  })

  #-------------------------------------------------------------------------------------------------------
  #Search Postcode button
  observeEvent(input$postcodes, {

    if(input$postcodes != "") {

    pc_data <- subset(temp_data,postcode==input$postcodes)

    #Create Address list
    address_list <- c('', as.character(pc_data$building_number)) #add empty address for starting value

    updateSelectInput(session,"addresses",choices = address_list, selected = "")

    google_map_update("map",data = pc_data) %>%
      clear_bounds() %>%
      clear_markers() %>%
      add_markers(lat = "lat", lon = "lon", update_map_view=TRUE, mouse_over = "building_number") %>%
      update_pano(pano = "pano", lat = pc_data$latc[1], lon = pc_data$lonc[1])

    }

  })

  #-------------------------------------------------------------------------------------------------------
  #What to do when address is selected
  observeEvent(input$addresses, {

    if(input$addresses != "") {

      address_row <- subset(temp_data,postcode==input$postcodes & building_number==input$addresses) #select address row

      google_map_update("map",data = address_row) %>%
        clear_bounds() %>%
        clear_markers() %>%
        add_markers(lat = "lat", lon = "lon", update_map_view=FALSE, mouse_over = "building_number") %>%
        update_pano(pano = "pano", lat = address_row$lat, lon = address_row$lon)

    }

  })
  #-------------------------------------------------------------------------------------------------------

} #server

#This allows the ui and server code to be in one file
shinyApp(ui, server)
rtaylor741 commented 5 years ago

Hi, I had another look at this and tweaked your javascript code slightly to fix it.

In google_maps.js, I changed the line below.

var center = map.getCenter();

to

var center = {lat: lat, lng: lon};

This then uses the variables that you input instead of moving it to the center.

Thanks.

maxlavoie commented 5 years ago

I was also looking for that feature. Any idea on the timeline? thanks for a great package! Martin

SymbolixAU commented 5 years ago

@maxlavoie I've made this suggested change, so it should be available in the github version now.

maxlavoie commented 5 years ago

Works perfectly. Thanks for doing this so quickly!

SymbolixAU commented 5 years ago

cool - I'll close this now as I think it's working as intented.

meldataaa commented 4 years ago

I am trying to use the update_pano function and I'm getting the "could not find function "update_pano" error". Is there a specific version of googleway I need to install? I currently have googlway 2.7.2 and googlepolylines 0.7.2.

dcooley commented 4 years ago

Can you try

remotes::install_github("SymbolixAU/googleway")

And see if it works from here directly?

(I may not have correctly updated the version numbers on github)

meldataaa commented 4 years ago

Thank you for the quick response but I’m still having the same error after your suggested install.

dcooley commented 3 years ago

For some reason I didn't @export the function in the latest version, so you currently have to use googleway:::update_pano() - with the 3 :::