经度和纬度的城市名称

City names from longitude and latitude

我尝试通过经度 lon 和纬度 lat 坐标获取城市名称。

 coords %>% str()
 'data.frame':  73 obs. of  2 variables:
 $ lon: Factor w/ 36 levels "-0.093093872070312",..: 12 24 21 13 21 13 15 6 
 21 21 ...
 $ lat: Factor w/ 36 levels "17.550094604492",..: 29 2 10 28 10 28 31 13 10 
 10 ...

这是输入:

structure(list(lon = structure(c(12L, 24L, 21L, 13L, 21L, 13L, 
15L, 6L, 21L, 21L, 23L, 36L, 12L, 10L, 28L, 14L, 22L, 34L, 21L, 
3L, 24L, 21L, 24L, 26L, 1L, 21L, 25L, 31L, 23L, 4L, 1L, 21L, 
12L, 13L, 21L, 21L, 5L, 33L, 17L, 11L, 24L, 24L, 21L, 29L, 26L, 
2L, 27L, 24L, 19L, 35L, 12L, 8L, 31L, 32L, 8L, 26L, 31L, 21L, 
18L, 21L, 16L, 12L, 31L, 21L, 20L, 9L, 7L, 23L, 30L, 21L, 21L, 
19L, 24L), .Label = c("-0.093093872070312", "-0.32969665527344", 
"-6.8442993164062", "-73.587699890137", "-74.00520324707", 
"-74.073303222656", 
"-79.463302612305", "10.75", "11.583297729492", "16.216705322266", 
"2.30029296875", "2.3332977294922", "2.3386993408203", "2.3488006591797", 
"2.3547973632812", "2.3560943603516", "2.4033050537109", "2.4893035888672", 
"3.877197265625", "35.368896484375", "35.509704589844", "35.649993896484", 
"35.833297729492", "39.219192504883", "44.399200439453", "46.715194702148", 
"5.1000061035156", "5.4956970214844", "51.533294677734", "54.36669921875", 
"55.304702758789", "7.1591033935547", "7.7873992919922", "77.216705322266", 
"8", "9.1999969482422"), class = "factor"), lat = structure(c(29L, 
2L, 10L, 28L, 10L, 28L, 31L, 13L, 10L, 10L, 9L, 18L, 29L, 22L, 
15L, 27L, 12L, 7L, 10L, 11L, 2L, 10L, 2L, 4L, 33L, 10L, 1L, 5L, 
9L, 19L, 33L, 10L, 29L, 28L, 10L, 10L, 14L, 23L, 24L, 26L, 2L, 
2L, 10L, 6L, 4L, 32L, 35L, 2L, 16L, 20L, 29L, 34L, 5L, 36L, 34L, 
4L, 5L, 10L, 30L, 10L, 25L, 29L, 5L, 10L, 8L, 21L, 17L, 9L, 3L, 
10L, 10L, 16L, 2L), .Label = c("17.550094604492", "21.516906738281", 
"24.466705322266", "24.653701782227", "25.258193969727", "25.286697387695", 
"28.666702270508", "33.563095092773", "33.833297729492", "33.87190246582", 
"34.013793945312", "34.11669921875", "4.6356964111328", "40.721405029297", 
"43.48779296875", "43.610900878906", "43.660507202148", "45.466705322266", 
"45.500900268555", "47", "48.149993896484", "48.38330078125", 
"48.600402832031", "48.787200927734", "48.83219909668", "48.841201782227", 
"48.853393554688", "48.858200073242", "48.86669921875", "48.901794433594", 
"48.934204101562", "51.445404052734", "51.514205932617", "59.916702270508", 
"61.25", "62.737503051758"), class = "factor")), .Names = c("lon", 
"lat"), row.names = c(NA, -73L), class = "data.frame")

我尝试了这个 post 的解决方案:

library(ggmap)
res <- lapply(with(coords, paste(lon, lat, sep = ",")), geocode, output = 
"more")

但我对所有值都收到此错误:

Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=2.3332977294922,48.86669921875&sensor=false
geocode failed with status ZERO_RESULTS, location = "2.3332977294922,48.86669921875"Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=39.219192504883,21.516906738281&sensor=false

geocode 可以对向量进行运算。所以就这样做:

geocode(paste(df$lat, df$lon, sep=','), output = 'more')['locality']

不匹配的坐标将 return NA.