简体   繁体   English

如何从R中的城市名称和国家/地区获取经度和纬度坐标?

[英]How to get the longitude and latitude coordinates from a city name and country in R?

I have a long list of city names and countries and I would like to plot them on a map. 我有一长串城市名称和国家/地区,我想在地图上绘制它们。 In order to do this I need the longitude and latitude information of each of the cities. 为了做到这一点,我需要每个城市的经度和纬度信息。

My table is called test and has the following structure: 我的表名为test ,具有以下结构:

Cityname  CountryCode
New York  US
Hamburg   DE
Amsterdam NL

With the following code I have successfully solved the problem. 使用以下代码我已成功解决了问题。

library(RJSONIO)
nrow <- nrow(test)
counter <- 1
test$lon[counter] <- 0
test$lat[counter] <- 0
while (counter <= nrow){
  CityName <- gsub(' ','%20',test$CityLong[counter]) #remove space for URLs
  CountryCode <- test$Country[counter]
  url <- paste(
    "http://nominatim.openstreetmap.org/search?city="
    , CityName
    , "&countrycodes="
    , CountryCode
    , "&limit=9&format=json"
    , sep="")
  x <- fromJSON(url)
  if(is.vector(x)){
    test$lon[counter] <- x[[1]]$lon
    test$lat[counter] <- x[[1]]$lat    
  }
  counter <- counter + 1
}

As this is calling an external service (openstreetmaps.org) it can take a while for larger datasets. 由于这是调用外部服务(openstreetmaps.org),因此对于较大的数据集可能需要一段时间。 However, you probably only do this once in a while when new cities have been added to the list. 但是,您可能只在新城市添加到列表中时偶尔执行此操作。

A few other options for you. 还有其他一些选择。

ggmaps ggmaps

ggmaps has a function geocode which uses Google Maps to geocode. ggmaps有一个功能geocode ,它使用谷歌地图进行地理编码。 This limits you to 2,500 per day. 这限制你每天2,500。

taRifx.geo taRifx.geo

taRifx.geo's latest version has a geocode function which uses either Google or Bing Maps to geocode. taRifx.geo的最新版本具有geocode功能,可使用Google或Bing Maps进行地理编码。 The Bing version requires you to use a (free) Bing account, but in return you can geocode way more entries. Bing版本要求您使用(免费)Bing帐户,但作为回报,您可以对更多条目进行地理编码。 Features in this version: 此版本的功能:

  • Service choice (Bing and Google Maps both supported) 服务选择(支持Bing和Google Maps)
  • Log-in support (particularly for Bing, which requires an account key but in exchange allows for an order of magnitude more daily requests) 登录支持(特别是对于Bing,它需要一个帐户密钥,但在交换中允许每天更多的请求)
  • Geocode a whole data.frame at a time, including some time-savers like ignoring any rows which have already been geocoded 一次对整个data.frame进行地理编码,包括一些节省时间的操作,例如忽略已经进行过地理编码的任何行
  • Robust batch geocoding (so that any error does not cause the whole data.frame's worth of geocoding to be lost, for bigger jobs) 强大的批量地理编码(这样任何错误都不会导致整个data.frame的地理编码丢失,对于更大的工作)
  • Route finding (travel times from point A to point B) 路线寻找(从A点到B点的旅行时间)

Try this I think it will better solution for this problem 试试这个我认为这将是解决这个问题的更好方法

 > library(ggmap) Loading required package: ggplot2 Google Maps API Terms of Service: http://developers.google.com/maps/terms. Please cite ggmap if you use it: see citation('ggmap') for details. #Now you can give city name or country name individually > geocode("hamburg") Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=hamburg&sensor=false lon lat 1 9.993682 53.55108 geocode("amsterdam") Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=amsterdam&sensor=false lon lat 1 4.895168 52.37022 > geocode("new york") Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=new+york&sensor=false lon lat 1 -74.00594 40.71278 

Try This... 尝试这个...

function geocodeAddress(geocoder, resultsMap) {
    var address = document.getElementById('address').value;
    geocoder.geocode({'address': address}, function(results, status) {
      if (status === 'OK') {
        resultsMap.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: resultsMap,
          position: results[0].geometry.location,
          draggable:true
        });
        var infowindow = new google.maps.InfoWindow({
            content: "Please drag this marker to your position.."
          });
          infowindow.open(resultsMap,marker);
         document.getElementById('lat').value=marker.getPosition().lat();
        document.getElementById('lng').value=marker.getPosition().lng();
         marker.addListener('drag', handleEvent);
marker.addListener('dragend', handleEvent);
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

get full code from here.. 从这里获取完整的代码..

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

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