简体   繁体   English

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

[英]R Shiny: Get longitude and latitude of a country

I want to get longitude and latitude for a particular country so that I can zoom to that country in the map. 我想获取特定国家/地区的经度和纬度,以便可以在地图上缩放到该国家/地区。 I am using leaflet in shiny.I know I can use "setView( longitude , latitude, zoom = 4)" . 我正在使用发亮的传单。我知道我可以使用“ setView(经度,纬度,缩放= 4)” But how do I get the longitude and latitude of the country? 但是,如何获得该国的经度和纬度?

I have written the following code so far: 到目前为止,我已经编写了以下代码:

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)

I wanted to zoom in to a country. 我想放大一个国家。 So I found a better way to do it. 因此,我找到了一种更好的方法。 Using fitBounds(map, lng1, lat1, lng2, lat2) 使用fitBounds(map,lng1,lat1,lng2,lat2)

The code is as follows: 代码如下:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM