我应用此代码从17000数据集中获取纬度和经度的区域名称:
setwd("C:/Users/Cat/Documents")
data <- na.omit
link <- matrix(NA, nrow = 17899, ncol=1)
duh <- list()
semi <- list()
add1 <- matrix(NA, ncol = 1, nrow = 17899)
add2 <- matrix(NA, ncol = 1, nrow = 17899)
add3 <- matrix(NA, ncol = 1, nrow = 17899)
for(i in 1:17889){
link[i, 1] <- paste("https://maps.googleapis.com/maps/api/geocode/xml?",
"latlng=", sub(i,3), "MY API KEY", sep="")
duh[i] <- list(readLines(link[i, 1]))
duh[[i]] <- xmlTreeParse(duh[[i]], useInternalNodes = TRUE)
duh[[i]] <- getNodeSet(duh[[i]])
semi[[i]] <- xmlToDataFrame(duh[[i]], stringsAsFactors = FALSE)
add1[1] <- semi[[i]][1, 1]
add2[2] <- semi[[i]][2, 1]
add3[3] <- semi[[i]][3, 1]
final <- cbind(add1, add2, add3)
}
我收到了一个错误:
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r"):
cannot open URL 'https://maps.googleapis.com/maps/api/geocode/xml?latlng=MY API KEY': HTTP status was '400 Bad Request.
答案 0 :(得分:0)
简单的for循环:
lon lat name
1 16.373819 48.20817 Vienna
2 13.404954 52.52001 Berlin
3 37.617300 55.75583 Moscow
4 139.691706 35.68949 Tokyo
5 9.801697 52.47807 Isernhagen
6 114.057865 22.54310 Shenzhen
library(ggmap)
register_key(key = "...") # Your billing-enabled Google Maps API key
loc <- c("Vienna", "Berlin", "Moscow", "Tokyo", "Isernhagen", "Shenzhen")
coord <- NULL
for (i in 1:length(loc)){
coord <- rbind(coord, cbind(geocode(loc[i]), name = loc[i]))
}