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

Error when calling map API multiple times #228

Open
chalioui opened this issue Dec 14, 2020 · 2 comments
Open

Error when calling map API multiple times #228

chalioui opened this issue Dec 14, 2020 · 2 comments

Comments

@chalioui
Copy link

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
Copy link
Collaborator

dcooley commented Dec 15, 2020

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants