Gecoding口语地名:零结果,但可以获得手动结果(R ggmap)

时间:2016-08-31 14:57:47

标签: r google-maps ggplot2 geocoding ggmap

我想知道印度尼西亚爪哇岛地区办事处的经纬度。地区是行政区域,如美国的州。我的大多数地理编码查询都会返回不准确的结果:纬度和经度是整个地区的,而不是地区办事处。然而,如果我手动在Google地图中输入查询,我会找到我想要的内容。

library("ggmap")
# list of district names
dists <- read.csv("../javaDistNames.csv")
# vector of queries for Google maps
queries <- paste("Kantor Bupati ", dists$distName, ", ", dists$distName, 
           ", ", dists$provinceName, ", Indonesia", sep="")
# impute latitude and longitude
dists[c("lon", "lat")] <- geocode(queries)

表达&#34; Kantor Bupati&#34;指印度尼西亚的民政事务处。

。例如,如果我输入&#34; Kantor Bupati BOGOR,BOGOR,JAWA BARAT,Indonesia&#34;进入谷歌地图,我发现district office:lat = -6.479745,lon = 106.824742。但地理编码returns:lat = -6.597147,lon = 106.806。那是20公里之外:对我的目的来说不够精确。

1 个答案:

答案 0 :(得分:1)

我已经解决了这个问题:我使用Google Places API作为SymbolixAU的建议。下面的矢量化函数将我们想要进行地理编码的口语地名作为参数,并使用ggmap geocode进行地理编码的非口语地名的第二个矢量。它返回纬度,经度和地点的名称。获取API密钥here

library("ggmap") # regular geocode function
library("RJSONIO") # read JSON

# API Key for Google Places
key <- # your key here

geoCodeColloquial <- function(queries, bases) {

  # need coordinates of base to focus search
  print("Getting coordinates of bases...")
  baseCoords <- geocode(bases, source="google")

  # request to Google Places
  print("Requesting coordinates of queries...")
  requests <- paste("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=",
                   baseCoords$lat, ",", baseCoords$lon, 
                   "&radius=50000&keyword=",
                   queries,
                   "&key=",
                   key,
                   sep="")

  # results from Google Places; take only top result for each query
  info <- lapply(requests, 
                   function(request) 
                     fromJSON(request)$results[[1]])

  # lat and lon
  coords <- lapply(info, function(i) i$geometry$location)

  # name of top result
  geoCodeNames <- lapply(info, function(i) i$name)
  geoCodeNamesDf <- data.frame(matrix(unlist(geoCodeNames),
                                      nrow=length(geoCodeNames), byrow=T))

  # add lat, lon, and discovered names to dataframe
  outDf <- data.frame(matrix(unlist(coords),
                                nrow=length(coords), byrow=T))
  names(outDf) <- c("lat", "lon")
  outDf["geoCodeName"] <- geoCodeNamesDf
  return(outDf)
}