1

我正在尝试检索城市的经度和纬度。我通过 openstreetmap.org 创建了一个使用 JSON 的脚本。

library("RJSONIO")
CityName <- "Rotterdam"
CountryCode <- "NL"
CityName <- gsub(' ','%20',CityName)

url <- paste(
  "http://nominatim.openstreetmap.org/search?city="
      , CityName
      , "&countrycodes="
      , CountryCode
      , "&limit=1&format=json"
    , sep="")
x <- fromJSON(url,simplify=FALSE)

x
[[1]]
[[1]]$place_id
[1] "98036666"

[[1]]$licence
[1] "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright"

[[1]]$osm_type
[1] "relation"

[[1]]$osm_id
[1] "324431"

[[1]]$boundingbox
[[1]]$boundingbox[[1]]
[1] "51.842113494873"

[[1]]$boundingbox[[2]]
[1] "52.0045318603516"

[[1]]$boundingbox[[3]]
[1] "3.94075202941895"

[[1]]$boundingbox[[4]]
[1] "4.60184574127197"


[[1]]$lat
[1] "51.9228958"

[[1]]$lon
[1] "4.4631727"

[[1]]$display_name
[1] "Rotterdam, Stadsregio Rotterdam, Zuid-Holland, Nederland"

[[1]]$class
[1] "boundary"

[[1]]$type
[1] "administrative"

[[1]]$icon
[1] "http://nominatim.openstreetmap.org/images/mapicons/poi_boundary_administrative.p.20.png"

我想提取$lonand $lat,但是当我这样做时,我收到以下错误消息:

x$lon
NULL
x$lat
NULL

有没有人知道我做错了什么,因此没有得到预期的结果,这看起来像:

x$lon
4.4631727
x$lat
51.9228958

有什么建议么?谢谢!

4

1 回答 1

1

您需要先访问第一个列表:

> x[[1]]$lat
[1] "51.9228958"
> x[[1]]$lon
[1] "4.4631727"
于 2012-12-13T14:31:13.573 回答