Vla*_*lad 7 r autocomplete shiny r-leaflet googleway
在谷歌地图中,搜索输入框会在用户输入时自动完成地址。有没有办法在 R Shiny 中通过访问自动完成值来做到这一点,以便与映射包一起使用?
有一个JavaScript方法在这里。我尝试在下面的代码中在 R Shiny 中使用此方法。SymbolixAU 指出使用google_map( search_box = TRUE )这是一个简单的解决方案。不幸的是,它在我的代码中不起作用,也因为我希望搜索框与地图分开。
下面的尝试在页面上按此顺序有文本输入my_address、文本输出copy_of_address和 googleway 地图my_map。
预期行为是让用户在 text input 中输入文本my_address,使用地址自动完成(这是有效的),地址将被复制到文本输出中copy_of_address(这仅显示输入的内容,而不是自动完成的版本),最后是地图以这个地址为中心。
看到输入框具有自动完成地址,但是地址和地图的副本仅使用用户输入文本。
在下面的代码中,替换MyKey为您的 google api 密钥(有时可以使用空字符串)。
library(shiny)
library(googleway)
key <- "MyKey"
set_key(key = key)
google_keys()
ui <- shiny::basicPage(
div(
textInput(inputId = "my_address", label = "")
,textOutput(outputId = "copy_of_address")
,HTML(paste0("
<script>
function initAutocomplete() {
new google.maps.places.Autocomplete(
(document.getElementById('my_address')),
{types: ['geocode']}
);
}
</script>
<script src='https://maps.googleapis.com/maps/api/js?key=", key,"&libraries=places&callback=initAutocomplete'
async defer></script>
"))
,google_mapOutput(outputId = "my_map")
)
)
server <- function(input, output) {
my_address <- reactive({
input$my_address
})
output$copy_of_address <- renderText({
my_address()
})
output$my_map <- renderGoogle_map({
my_address <- my_address()
validate(
need(my_address, "Address not available")
)
df <- google_geocode(address = my_address)
my_coords <- geocode_coordinates(df)
my_coords <- c(my_coords$lat[1], my_coords$lng[1])
google_map(
location = my_coords,
zoom = 12,
map_type_control = FALSE,
zoom_control = FALSE,
street_view_control = FALSE
)
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
这对我来说并不容易弄清楚,但我认为我有一个合理的解决方案。最终归结为非常仔细地复制和粘贴 Google 的示例,阅读有关从 Javascript 向 Shiny 发送消息的内容,将想法整合在一起,并在第 200 次尝试中获得幸运……
在到期时给予信用:
谷歌的链接:https : //developers.google.com/maps/documentation/javascript/examples/places-autocomplete
RStudio 的链接:https ://shiny.rstudio.com/articles/js-send-message.html
我的潜在解决方案:
library(shiny)
library(googleway)
key <- "MyKey"
set_key(key = key)
#google_keys()
ui <- shiny::basicPage(
div(
textInput(inputId = "my_address", label = "Type An Address")
,textOutput(outputId = "full_address")
,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>"))
,google_mapOutput(outputId = "my_map")
)
)
server <- function(input, output) {
my_address <- reactive({
if(!is.null(input$jsValueAddressNumber)){
if(length(grep(pattern = input$jsValueAddressNumber, x = input$jsValuePretty ))==0){
final_address<- c(input$jsValueAddressNumber, input$jsValuePretty)
} else{
final_address<- input$jsValuePretty
}
final_address
}
})
output$full_address <- renderText({
if(!is.null(my_address())){
my_address()
}
})
output$my_map <- renderGoogle_map({
my_address <- my_address()
shiny::validate(
need(my_address, "Address not available")
)
not_a_df <- google_geocode(address = my_address)
my_coords <- geocode_coordinates(not_a_df)
my_coords <- c(my_coords$lat[1], my_coords$lng[1])
google_map(
location = my_coords,
zoom = 12,
map_type_control = FALSE,
zoom_control = FALSE,
street_view_control = FALSE
)
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助。干杯! --nate