使用googleway与ggmap的geocode函数处理邮政编码地理编码报错求助
问题:使用ggmap和googleway进行邮政编码地理编码时出错
一、ggmap代码及错误
library(ggmap) library(tidyverse) # 创建邮政编码数据框 data.new <- c(21022, 20380, 22194, 20414, 20402, 22350, 20451, 56998, 88888, 20417, 75059, 90210, 11437, 10101, 10275, 10269, 10111, 98490) data.new <- data.frame(data.new) data.new <- data.new %>% rename(zipcode=data.new) data.new$zipcode <- as.character(data.new$zipcode) register_google(key = "api_key") data.new_ggmap <- geocode(location = data.new$zipcode, output = "latlona", source = "google") data.new_ggmap <- cbind(data.new, data.new_ggmap) # 打印结果 data.new_ggmaps[, 1:18]
错误信息:
Warning: Geocoding "20380" failed with error:
二、googleway代码及错误
###OPTION2############################################################## library(googleway) library(tidyverse) google_api_key <- "api_key" # 创建邮政编码数据框 data.new <- c(21022, 20380, 22194, 20414, 20402, 22350, 20451, 56998, 88888, 20417, 75059, 90210, 11437, 10101, 10275, 10269, 10111, 98490) data.new <- data.frame(data.new) data.new <- data.new %>% rename(zipcode=data.new) data.new$zipcode <- as.character(data.new$zipcode) # 初始化存储谷歌数据的列 data.new$goog_address_components_ <- NA data.new$goog_formatted_address_ <- NA data.new$goog_geometry_ <- NA data.new$goog_place_id_ <- NA data.new$goog_types_ <- NA data.new$latitude <- NA data.new$longitude <- NA # 循环遍历数据框,将每个邮政编码传入谷歌API for(k in 1:nrow(data.new)){ # 打印循环索引查看进度 print(k) # 使用tryCatch捕获错误,遇到问题时跳过当前项继续执行 tryCatch({ # 定义搜索字符串为当前邮政编码 search_string <- data.new$zipcode[k] # 调用googleway的地理编码函数,需使用自己的谷歌API密钥 response<- google_geocode(search_string, key = google_api_key) # 从API响应中提取结果 results_df <- response$results # 将API响应结果添加到现有数据框 data.new$goog_address_components_[k] <- results_df$address_components data.new$goog_formatted_address_[k] <- results_df$formatted_address data.new$goog_geometry_[k] <- results_df$geometry data.new$goog_place_id_[k] <- results_df$place_id data.new$goog_types_[k] <- results_df$types }, error = function(e){cat("ERROR :",conditionMessage(e), "\n")}) } #########################################################################
错误信息:
[1] 1 [1] 2 ERROR : replacement has 17 rows, data has 18 [1] 3 ERROR : replacement has 17 rows, data has 18 [1] 4 ERROR : replacement has 17 rows, data has 18 [1] 5 ERROR : replacement has 17 rows, data has 18 [1] 6 ERROR : replacement has 17 rows, data has 18 [1] 7 ERROR : replacement has 17 rows, data has 18 [1] 8 ERROR : replacement has 17 rows, data has 18 [1] 9 ERROR : replacement has 17 rows, data has 18 [1] 10 ERROR : replacement has 17 rows, data has 18 [1] 11 ERROR : replacement has 17 rows, data has 18 [1] 12 ERROR : replacement has 17 rows, data has 18 [1] 13 ERROR : replacement has 17 rows, data has 18 [1] 14 ERROR : replacement has 17 rows, data has 18 [1] 15 ERROR : replacement has 17 rows, data has 18 [1] 16 ERROR : replacement has 17 rows, data has 18 [1] 17 ERROR : replacement has 17 rows, data has 18 [1] 18 ERROR : replacement has 17 rows, data has 18 Warning message: In data.new$goog_geometry_[k] <- results_df$geometry : number of items to replace is not a multiple of replacement length
修复方案
针对googleway的错误修复
错误根源是response$results中的address_components、geometry等字段是嵌套列表结构,直接赋值给数据框的普通列会导致长度不匹配。只需提取实际需要的字段(比如经纬度)即可解决:
library(googleway) library(tidyverse) google_api_key <- "你的谷歌API密钥" # 创建邮政编码数据框 data.new <- tibble(zipcode = as.character(c(21022, 20380, 22194, 20414, 20402, 22350, 20451, 56998, 88888, 20417, 75059, 90210, 11437, 10101, 10275, 10269, 10111, 98490))) # 循环处理每个邮政编码,提取关键信息 for(k in 1:nrow(data.new)){ print(k) tryCatch({ search_string <- data.new$zipcode[k] response <- google_geocode(search_string, key = google_api_key) # 仅当API返回有效结果时提取数据 if(nrow(response$results) > 0){ data.new$latitude[k] <- response$results$geometry$location$lat data.new$longitude[k] <- response$results$geometry$location$lng data.new$formatted_address[k] <- response$results$formatted_address data.new$place_id[k] <- response$results$place_id } }, error = function(e){cat("ERROR :",conditionMessage(e), "\n")}) } # 查看最终结果 print(data.new)
针对ggmap的错误修复
常见错误原因包括:API密钥未启用地理编码服务、无效邮政编码、API配额不足或权限限制。修复步骤如下:
- 登录谷歌云控制台,确保「地理编码API」已启用;
- 检查API密钥权限,允许当前家用IP访问(可临时设置为不限IP,注意密钥安全);
- 过滤无效邮编(比如
88888是虚构邮编),或在编码时处理失败项;
修改后的ggmap代码:
library(ggmap) library(tidyverse) register_google(key = "你的谷歌API密钥") data.new <- tibble(zipcode = as.character(c(21022, 20380, 22194, 20414, 20402, 22350, 20451, 56998, 88888, 20417, 75059, 90210, 11437, 10101, 10275, 10269, 10111, 98490))) # 批量地理编码,自动处理失败项 data.new_ggmap <- data.new %>% mutate(geo_result = map(zipcode, ~{ tryCatch(geocode(location = ., output = "latlona", source = "google"), error = function(e) tibble(lat=NA, lon=NA, address=NA, type=NA)) })) %>% unnest(geo_result) # 查看结果 print(data.new_ggmap)
内容的提问来源于stack exchange,提问作者Kenny
相关产品推荐
相关产品推荐

