假如data.frame data中的lat值為0時,則利用google maps api geocode 查詢地址的經緯座標,並存回 data$lat 與 data$lng 中:
library(ggmap)
for (i in 1:nrow(data)) {
if (data$lat[i] == 0) {
addr <- data$service_addr[i]
url = paste('http://maps.google.com/maps/api/geocode/xml?address=', addr,'&sensor=false',sep='')
doc = xmlTreeParse(url)
root = xmlRoot(doc)
data$lat[i] = xmlValue(root[['result']][['geometry']][['location']][['lat']])
data$lng[i] = xmlValue(root[['result']][['geometry']][['location']][['lng']])
}
}
# 將 data 內容寫入檔案 mymap.csv
write.table(data, file = "mymap.csv", sep = "," )
#
沒有留言:
張貼留言