在R中从谷歌地图中删除城市和州

在R中从谷歌地图中删除城市和州,r,curl,web-scraping,css-selectors,rvest,R,Curl,Web Scraping,Css Selectors,Rvest,我正在做一个项目,需要一些基本的位置信息,但相关信息很少。我试着用R写一个函数或一系列行,取一个地标的名字,在谷歌地图搜索中使用这个名字,然后从网站上搜索地标所在的城市和州 范例 landmark.data <- data.frame(landmark.name = c("Packer Stadium", "University of Chicago", "Disney World")) findCityState(landmark.data) # Insert amazing funct

我正在做一个项目,需要一些基本的位置信息,但相关信息很少。我试着用R写一个函数或一系列行,取一个地标的名字,在谷歌地图搜索中使用这个名字,然后从网站上搜索地标所在的城市和州

范例

landmark.data <- data.frame(landmark.name = c("Packer Stadium", "University of Chicago", "Disney World"))
findCityState(landmark.data) # Insert amazing function here

  landmark.city landmark.state
1     Green Bay             WI
2       Chicago             IL
3       Orlando             FL

landmark.data考虑到谷歌有一个API供您访问,您可能不需要刮取数据

我的Googleway软件包为其中大多数提供了方便的功能,其中之一就是地理编码

library(googleway)

## you need an api key to use their service
key <- 'your_api_key'

landmark.data <- c("Packer Stadium", "University of Chicago", "Disney World")

lst_result <- lapply(landmark.data, function(x){
    google_geocode(x, key = key)
})
在这里,让我们看看地图上的结果到底给了我们什么

mapKey <- symbolix.utils::mapKey()

lst_coordinates <- lapply(lst_result, function(x){
    x[['results']][['geometry']][['location']]
})

coordinates <- do.call('rbind', lst_coordinates)

google_map(key = mapKey) %>%
    add_markers(coordinates)

mapKey请dput()您的数据。请查看ggmap包和geocode函数。这可能会在一个漂亮的R包装中提供您想要的信息。这很好。谢谢API限制每天查询2500次,这让我有点沮丧,这就是为什么我最初将web抓取作为解决此问题的一种方法。@KyleFurlong-不客气。还有其他的地理编码服务,比如Data Science Tookkit(
RSDK
package),它是免费的,在美国也可以使用。此外,我认为抓取违反了谷歌的服务条款(因此API)
mapKey <- symbolix.utils::mapKey()

lst_coordinates <- lapply(lst_result, function(x){
    x[['results']][['geometry']][['location']]
})

coordinates <- do.call('rbind', lst_coordinates)

google_map(key = mapKey) %>%
    add_markers(coordinates)