這可能是一個相當隨機/隱晦的問題,但它是否包含一個函數,其中包含一個將美國City
和State
(例如「CA」)輸入轉換爲字符串並返回經度&緯度座標?從城市和州輸入獲取經度和緯度的快速方法
0
A
回答
3
https://maps.googleapis.com/maps/api/geocode/json?address={city},{state}
......當然可以用您正在搜索的城市/州替換{city}和{state}。它返回一個JSON字符串,所以你可以通過ajax進行這個調用並執行JS處理。
5
UPDATE
或者你可以做一個devtools::install_github("hrbrmstr/localgeo")
並運行它geocode
(剛建的話)。它沒有rgdal
,rgeos
或httr
依賴關係,只有dplyr
。
而且this place有ZIP /市/州/經度的自由CSV文件/ LAT你可以只match
或dplyr::left_join
無需使用API:
library(rgeos)
library(rgdal)
library(httr)
library(dplyr)
# httr's write_disk can act like a cache as it won't download if
# the file exists
GET("http://www.mapcruzin.com/fcc-wireless-shapefiles/cities-towns.zip",
write_disk("cities.zip"))
unzip("cities.zip", exdir="cities")
# read in the shapefile
shp <- readOGR("cities/citiesx020.shp", "citiesx020")
# extract the city centroids with name and state
geo <-
gCentroid(shp, byid=TRUE) %>%
data.frame() %>%
rename(lon=x, lat=y) %>%
mutate([email protected]$NAME, [email protected]$STATE)
# lookup!
geo %>% filter(city=="Portland", state=="ME")
## lon lat city state
## 1 -70.25404 43.66186 Portland ME
geo %>% filter(city=="Berwick", state=="ME")
## lon lat city state
## 1 -70.86323 43.26593 Berwick ME
有可能用這些屬性來更加全面的形狀文件。這個城鎮有28,706個城市,所以看起來相當全面。
那些可以很容易地在函數中,使用更簡便:
geo_init <- function() {
try({
GET("http://www.mapcruzin.com/fcc-wireless-shapefiles/cities-towns.zip",
write_disk("cities.zip"))
unzip("cities.zip", exdir="cities") })
shp <- readOGR("cities/citiesx020.shp", "citiesx020")
geo <-
gCentroid(shp, byid=TRUE) %>%
data.frame() %>%
rename(lon=x, lat=y) %>%
mutate([email protected]$NAME, [email protected]$STATE)
}
geocode <- function(geo_db, city, state) {
do.call(rbind.data.frame, mapply(function(x, y) {
geo_db %>% filter(city==x, state==y)
}, city, state, SIMPLIFY=FALSE))
}
geo_db <- geo_init()
geo_db %>% geocode("Portland", "ME")
## lon lat city state
## Portland -70.25404 43.66186 Portland ME
geo_db %>%
geocode(c("Portland", "Berwick", "Alfred"), "ME")
## lon lat city state
## Portland -70.25404 43.66186 Portland ME
## Berwick -70.86323 43.26593 Berwick ME
## Alfred -70.71754 43.47681 Alfred ME
geo_db %>%
geocode(city=c("Baltimore", "Pittsburgh", "Houston"),
state=c("MD", "PA", "TX"))
## lon lat city state
## Baltimore -76.61158 39.29076 Baltimore MD
## Pittsburgh -79.99538 40.44091 Pittsburgh PA
## Houston -95.36400 29.76376 Houston TX
2
擴大@ ZacWolf的答案,你可以做到這一點使用我的googleway包和谷歌地圖API(您需要一個API密鑰)
library(googleway)
## your api key
key <- read.dcf("~/Documents/.googleAPI", fields = "GOOGLE_API_KEY")
google_geocode(address = "Los Angeles, California", key = key)
# $results
# address_components
# 1 Los Angeles, Los Angeles County, California, United States, Los Angeles, Los Angeles County, CA, US, locality, political, administrative_area_level_2, political, administrative_area_level_1, political, country, political
# formatted_address geometry.bounds.northeast.lat geometry.bounds.northeast.lng geometry.bounds.southwest.lat geometry.bounds.southwest.lng
# 1 Los Angeles, CA, USA 34.33731 -118.1553 33.70369 -118.6682
# geometry.location.lat geometry.location.lng geometry.location_type geometry.viewport.northeast.lat geometry.viewport.northeast.lng
# 1 34.05223 -118.2437 APPROXIMATE 34.33731 -118.1553
# geometry.viewport.southwest.lat geometry.viewport.southwest.lng place_id types
# 1 33.70369 -118.6682 ChIJE9on3F3HwoAR9AhGJW_fL-I locality, political
如果你有多個城市/州
df <- data.frame(city = c("Portland", "Houston", "Pittsburg"),
state = c("ME", "TX", "PA"))
res <- apply(df, 1, function(x) {
google_geocode(address = paste0(x["city"], ", ", x["state"]), key = key)
})
lapply(res, function(x) x[['results']][['geometry']][['location']])
# [[1]]
# lat lng
# 1 43.66147 -70.25533
#
# [[2]]
# lat lng
# 1 29.76043 -95.3698
#
# [[3]]
# lat lng
# 1 40.44062 -79.99589
+0
...而反對票是因爲? – SymbolixAU 2016-11-12 11:24:05
1
ggmap::geocode
包裝了谷歌和數據科學工具包的地理編碼的API整齊:
df <- data.frame(city = c('Washington', 'Los Angeles'),
state = c('DC', 'CA'))
df <- cbind(df, geocode(paste(df$city, df$state)))
df
## city state lon lat
## 1 Washington DC -77.03687 38.90719
## 2 Los Angeles CA -118.24368 34.05223
它還包括一個mutate_geocode
版本,如果你喜歡dplyr
語法,雖然詳細地址已被完全組裝爲列:
library(dplyr)
df <- df %>% mutate(address = paste(city, state)) %>% mutate_geocode(address)
df
## city state address lon lat
## 1 Washington DC Washington DC -77.03687 38.90719
## 2 Los Angeles CA Los Angeles CA -118.24368 34.05223
相關問題
- 1. 從經緯度獲取城市,州,國家
- 2. 從城市名稱獲取經緯度
- 3. 從經緯度獲取城市名稱
- 4. 按城市或經度和緯度
- 5. 獲取特定城市的緯度和經度android
- 6. iPhone中的Google地圖 - 經度和緯度的城市和州名稱
- 7. 免費城市/州/郵編到緯度/經度數據庫?
- 8. 從給定的城市名稱獲取經度和緯度的簡單API
- 9. 過濾城市的經緯度與其他城市的距離(經緯度)?
- 10. 獲取緯度和經度
- 11. 獲取經度和緯度
- 12. Facebook的圖表獲取用戶城市的緯度和經度longetude
- 13. 獲取某個國家(或州)的所有城市的列表,包括經度和緯度
- 14. 獲取基於城市,郵編或街道名稱的經度和緯度
- 15. android從輸入位置獲取緯度和經度
- 16. 從隆齊和緯度獲取城市的人口
- 17. 無法獲取經度和緯度android
- 18. 從GpsMyLocationProvider獲取經度和緯度(OSMdroid)
- 19. 獲取緯度和經度從PIN碼
- 20. 從restfb獲取緯度和經度
- 21. 從PostCode獲取經度和緯度
- 22. 緯度和經度放置在.NET中的名稱或城市
- 23. 如何使用緯度和經度獲取位置名稱或城市?
- 24. 想要使用城市的經度和緯度來表示我們城市的城市
- 25. 從經緯度得到國家,地區和城市名稱(PHP)
- 26. 從特定語言的緯度,經度獲取城市名稱javascript api
- 27. 如何使用Facebook API從經度/緯度獲取城市的地點編號
- 28. 獲取經緯度範圍內的所有城市
- 29. 如何獲取經緯度的城市信息
- 30. 獲取經度和緯度的限制
Google maps api? – 2015-01-09 19:35:43
也許您可以使用此網站的網絡服務http://www.gpsvisualizer.com/geocoder/ – ckluss 2015-01-09 19:35:47
請參閱https://stackoverflow.com/questions/3257441/geocoding-in-r-with-google-maps或http://blog.corynissen.com/2014/10/making-r-package-to-use-here-geocode-api.html或https://stackoverflow.com/questions/22887833/r-how-to- geocode-a-simple-address-using-data-science-toolbox – ckluss 2015-01-09 19:36:46