简体   繁体   English

如何从城市名称和邮政编码中获取经纬度数据

[英]How to fetch latitude and longitude data from city name and zipcode

I am looking for a way to get latitude and longitude coordinates form an indian zip code in R.我正在寻找一种方法来从 R 中的印度邮政编码中获取纬度和经度坐标。

My data frame looks like:我的数据框看起来像:

Area       Zip   
Mumbai    410710
Mumbai    400083
Mumbai    400034
Mumbai    400612

Looking to get:期待获得:

Area       Zip         Latitude     Longitude
Mumbai    410710        ...           ...
Mumbai    400083
Mumbai    400034
Mumbai    400612

You can use ggmap::geocode .您可以使用ggmap::geocode

library(ggmap)    

zip <- read.table(text = "Area       Zip   
    Mumbai    410710
    Mumbai    400083
    Mumbai    400034
    Mumbai    400612", header = T)

gc <- do.call(rbind,
        apply(zip, 1, function(x){geocode(location = paste(x[2], x[1], sep = ", "), 
                                          source = "google")})
        )
cbind(zip, gc)
#     Area    Zip      lon      lat
# 1 Mumbai 410710 72.87766 19.07598
# 2 Mumbai 400083 72.93742 19.11662
# 3 Mumbai 400034 72.81482 18.97242
# 4 Mumbai 400612 73.02872 19.17993

However, sometimes the Google API seems to return NA for no reason.但是,有时 Google API 似乎会无缘无故地返回NA Therefore, you might have to loop through it until no NA is contained anymore.因此,您可能必须循环遍历它,直到不再包含NA
Or use source = "dsk" .或者使用source = "dsk" However, the datasciencetoolkit might not have long lat for all these zip codes.但是,datasciencetoolkit 可能没有所有这些邮政编码的长纬度。

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

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