0

我正在尝试利用 Google 的 API 的下拉搜索功能来使用 Leaflet 映射选定的地址。但是,我也有一个现有的数据集,我想基于复选框输入将其添加到传单地图中。这意味着两者都需要反应,而且我很难重新绑定这些数据。

我使用了来自对R Shiny 地图搜索输入框的响应的代码,以便在创建 Google 下拉列表的 ui 函数中创建代码。

我正在使用 RCurl 从 Google Drive 中提取数据集

library(RCurl)
library(shiny)
library(googleway)
myCsv <- getURL("https://docs.google.com/spreadsheets/d/e/2PACX-1vTc6H0ikgU53DJsGWzaSJO__77xIVkwAbyI2vjkYuHSAjj9hzWVtVAOhah8B5baDO4ilfqJTm5OWFzW/pub?output=csv")
location_data = read.csv(textConnection(myCsv))

或者如果更容易通过表格阅读:

Locations Latitude Longitude
location1 40.72993 -74.00022
location2 40.72993 -74.00022
location3 40.67678 -73.98048
location4 40.76954 -73.95436
location5 40.72504 -73.98145
location6 40.80251 -73.96745

我的用户界面功能:

ui <- basicPage(
  div(
    checkboxInput("checkbox", "Map Other Locations", value = FALSE),
    textInput(inputId = "my_address", label = "Type An Address")    
    ,textOutput(outputId = "full_address")

    #Google HTML dropdown code
    ,HTML(paste0(" <script> 
                function initAutocomplete() {

                 var autocomplete =   new google.maps.places.Autocomplete(document.getElementById('my_address'),{types: ['geocode']});
                 autocomplete.setFields(['address_components', 'formatted_address',  'geometry', 'icon', 'name']);
                 autocomplete.addListener('place_changed', function() {
                 var place = autocomplete.getPlace();
                 if (!place.geometry) {
                 return;
                 }

                 var addressPretty = place.formatted_address;
                 var address = '';
                 if (place.address_components) {
                 address = [
                 (place.address_components[0] && place.address_components[0].short_name || ''),
                 (place.address_components[1] && place.address_components[1].short_name || ''),
                 (place.address_components[2] && place.address_components[2].short_name || ''),
                 (place.address_components[3] && place.address_components[3].short_name || ''),
                 (place.address_components[4] && place.address_components[4].short_name || ''),
                 (place.address_components[5] && place.address_components[5].short_name || ''),
                 (place.address_components[6] && place.address_components[6].short_name || ''),
                 (place.address_components[7] && place.address_components[7].short_name || '')
                 ].join(' ');
                 }
                 var address_number =''
                 address_number = [(place.address_components[0] && place.address_components[0].short_name || '')]
                 var coords = place.geometry.location;
                 //console.log(address);
                 Shiny.onInputChange('jsValue', address);
                 Shiny.onInputChange('jsValueAddressNumber', address_number);
                 Shiny.onInputChange('jsValuePretty', addressPretty);
                 Shiny.onInputChange('jsValueCoords', coords);});}
                 </script> 
                 <script src='https://maps.googleapis.com/maps/api/js?key=", key,"&libraries=places&callback=initAutocomplete' async defer></script>"))

     #map output
     ,leafletOutput("mymap",height = 600)
  )

)

我的服务器代码:

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

  #if checkbox is checked, rbind location + google data
  #if checkbox not checked, provide google data
  location_data_reactive <- reactive({
    if (input$checkbox == TRUE) { 
        input_address <- input$my_address
        not_a_df <- google_geocode(address = my_address)
        coords <- geocode_coordinates(not_a_df)
        coords_to_rbind <- c(as.character("Input Location"), coords$lat[1], coords$lng[1])
        data_to_map <- rbind(location_data, test_to_rbind)
        final_data <- rbind(location_data, my_coords)
    } else if (input$checkbox == FALSE) { 
        input_address <- input$my_address
        not_a_df <- google_geocode(address = my_address)
        coords <- geocode_coordinates(not_a_df)
        final_data <- c(coords$lat[1], coords$lng[1])
        colnames(final_data) <- c("Latitude", "Longitude")
    } else { location_data[0,] }
  })



  #leaflet map
  output$mymap <- renderLeaflet({
    location_data_to_map <- location_data_reactive()

    m <- leaflet(data = location_data_to_map) %>%
      addProviderTiles(providers$CartoDB.Positron) %>%
      addAwesomeMarkers(lng = ~Longitude,
                        lat = ~Latitude
      )
    m
    })

}  

shinyApp(ui, server)

对于我尝试做的所有事情,我都会收到以下错误消息,并通过堆叠这些数据或重新排列 if 语句来运行。

Error: address must be a string of length 1

任何人都可以协助堆叠这两个数据反应数据帧吗?谢谢!

4

1 回答 1

0

我正在解决同样的问题。这是为我解决的问题:

  1. 确保您具有对 Google Geocoding API 的 API 访问权限(它与代码上部使用的 Places API 不同):https ://developers.google.com/maps/documentation/geocoding/start
  2. 使用 google_geocode(address = my_address())(注意额外的括号)
于 2020-09-08T04:51:36.203 回答