SymbolixAU / googleway

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

Error when calling map API multiple times #228

Open chalioui opened 3 years ago

chalioui commented 3 years ago

Hi there! Thank you for this great package! I want to use autocomplete in shiny textInput, and display the location on a google map:

 textInput("someaddress",label = NULL,width = "100%"),
                HTML(paste0(" <script> 
                function initAutocomplete() {
                 var autocomplete =   new google.maps.places.Autocomplete(document.getElementById('someaddress'));
                 autocomplete.addListener('place_changed', function() {
                 var place = autocomplete.getPlace();
                 Shiny.setInputValue('jsName', place.name);
                 Shiny.setInputValue('jsAddress', place.formatted_address);     
                 });
                 } </script>
                <script src='https://maps.googleapis.com/maps/api/js?key=", map_key2, "&libraries=places&callback=initAutocomplete' async defer></script>"))

It works but I have an error message on the console of my browser : You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors (and yes some times the map is not visible)

image

dcooley commented 3 years ago

This error looks like it's caused by including

<script src='https://maps.googleapis.com/maps/api/js?key=", map_key2, "&libraries=places&callback=initAutocomplete' async defer></script>"))

as this script is already loaded when you call google_map().

Have you looked into this other issue and solution as a way to do this?

chalioui commented 3 years ago

Yes this this the part of code that causes the error, my app works good when I remove it. I tried the solution :

shinyApp(
  ui = fluidPage(
    shiny::titlePanel("A Shiny google maps search box"),
    fluidRow(column(
      3, autocomplete_input("auto", NULL, opts[1:10], max_options = 250, create = TRUE)
    ))
  ),
  server = function(input, output, session) {
    observeEvent(input$auto, {
      optPlaces <- googleway::google_place_autocomplete(input$auto, key = "xxxxxxxxxxx")
      update_autocomplete_input(session, "auto", options = optPlaces$predictions[,"description"])
    }, ignoreInit = TRUE)
  }
)

The autocompletion is slow but works partialy : provides suggestions but I get an error when I clear the input : Error in [: incorrect number of dimensions