R Shiny:获取一个国家的经度和纬度

时间:2016-06-09 13:50:37

标签: r shiny leaflet

我希望获得特定国家/地区的经度和纬度,以便我可以在地图中缩放到该国家/地区。我正在使用有光泽的传单。我知道我可以使用" setView(经度,纬度,缩放= 4)" 。但我如何获得该国的经度和纬度呢?

到目前为止,我编写了以下代码:

library(shiny)
library(leaflet)

ui <- fluidPage(

   leafletOutput("CountryMap", width = 1000, height = 500)
)

server <- function(input, output){

   Country = map("world", fill = TRUE, plot = FALSE, regions="USA")
   output$CountryMap <- renderLeaflet({

  leaflet(Country) %>% addTiles() %>%  setView( 0 , 0, zoom = 4)%>%
   addPolygons(fillOpacity = 0.6,  smoothFactor = 0.5, stroke = TRUE, weight = 1)
})
}


shinyApp(ui =ui, server = server)

1 个答案:

答案 0 :(得分:0)

我想放大一个国家。所以我发现了一种更好的方法。使用 fitBounds(map,lng1,lat1,lng2,lat2)

代码如下:

library(shiny)
library(leaflet)
library(maps)

ui <- fluidPage(

  leafletOutput("CountryMap", width = 1000, height = 500)
)

server <- function(input, output){

 Country = map("world", fill = TRUE, plot = FALSE, regions="USA", exact=TRUE)
 output$CountryMap <- renderLeaflet({
     leaflet(Country) %>% addTiles() %>%
     fitBounds(Country$range[1], Country$range[3], Country$range[2], Country$range[4])%>%
     addPolygons(fillOpacity = 0.6,  smoothFactor = 0.5, stroke = TRUE, weight = 1)
})
}


shinyApp(ui =ui, server = server)