Closed noamanemobidata closed 3 years ago
Adding this script
<script src='https://maps.googleapis.com/maps/api/js?key=", map_key, "&libraries=places&callback=initAutocomplete' async defer ></script>"))
is giving you a conflict, because that script is already loaded when you call google_map()
in the libraries
argument
The solution:
Remove that script, and the surrounding function it uses as the callback
showModal(
modalDialog(footer = NULL, easyClose = T,
fluidPage(
HTML('<div class="form-group">
<input id="AdressNew" type="text" class="form-control" placeholder="Enter some adress">
</div> '),
HTML(paste0(" <script>
var autocomplete = new google.maps.places.Autocomplete(document.getElementById('AdressNew'));
autocomplete.addListener('place_changed', function() {
autocomplete.setFields(['place_id', 'geometry', 'name']);
var place = autocomplete.getPlace();
Shiny.setInputValue('jsGeometry', place.geometry);
});
</script>"
))
),
actionButton(inputId = 'go',label = 'submit')
)
)
I've also added the geometry
field to the list of objects returned from the Auto-complete field, so you get the lon & lat directly, without having to geocode:
observeEvent(input$go,{
df <- data.frame(
lat = input$jsGeometry$location$lat,
lng = input$jsGeometry$location$lng
)
google_map_update(map_id = "map") %>%
googleway::add_markers(data = df, lat = "lat", lon = "lng", update_map_view = T)
})
Thank you for this question; It made me finally understand how to properly use the auto-complete within a shiny :)
Hi Dave! wonderful solution! works fine thank you very mush :)
Hi! I'm try to add a maker to the google map, the coordinates are calculated from geocoded address string provided by the user inside a modal:
this is the app code
ps: works fine without using modal ( with address input in sidebar)
Thank you