高德地图api根据经纬度获取地址_【R语言】利用高德地图批量处理银行网点地址获取经纬度和行政区...

高德地图密钥Key的申请

01

查看后期视频:高德地图申请密钥key

密钥Key如何添加至RStudio?

usethis::edit_r_environ()#打开environ界面key="2ad35axxxxxxx"#定义完一定要保存key "key")

构建获取location的经纬度函数

>library(xml2)>library(rvest)>library(dplyr)>library(stringr)>library(rjson)>library(jsonlite)>gGetLocation = function(address)  {>key "key")>url =paste0("https://restapi.amap.com/v3/geocode/geo?key=",key,"&address=",address)>data = read_html(url, encoding='utf-8') %>% html_text()>df = as.data.frame(fromJSON(data))    return (df['geocodes.location']) }# test,测试> gGetLocation('南京市')      geocodes.location1 118.796877,32.060255

构建获取district的区县函数

gGetdistrictFuZhou = function(address)  {  key "lxl")  url = paste0("https://restapi.amap.com/v3/geocode/geo?key=",key,"&city=","福州市","&address=",address)  data = read_html(url, encoding='utf-8') %>% html_text()  df = as.data.frame(fromJSON(data))    return (df['geocodes.district']) }gGetdistrictFuZhou = function(address)  {  key "lxl")  url = paste0("https://restapi.amap.com/v3/geocode/geo?key=",key,"&city=","福州市","&address=",address)  data = read_html(url, encoding='utf-8') %>% html_text()  df = as.data.frame(fromJSON(data))    return (df['geocodes.district']) }#test 测试>gGetdistrictFuZhou("凤城镇丹凤东路25号海联大厦1层")  geocodes.district1            连江县

批量处理

数据源结构特点:

333f912f6a92ed4a438ea938d303ebd5.png

图1 数据源结构

代码:

library(readxl)library(dplyr)library(stringr)library(tidyverse)library(openxlsx)YH "~/Desktop/YH.xlsx")YH$经纬度 YH$区县 for(i in 1:nrow(YH)){  print(i)  if (is.na(YH$经纬度[i])) {    try({YH$经纬度[i]   if(is.na(YH$区县[i])){    try({YH$区县[i]  }  }}YH%>%separate(col=经纬度",into=c("lon","lat"),sep=",")->YHwrite.xlsx(YH,"~/Desktop/微信公众号运营/银行网点.xlsx")

批量处理后数据结构:

c0a08b91e8f8c881e4d74a0ed2666272.png

图2 数据源结构

批量处理后的数据暂无缺失值,若存在个别缺失值很正常,通常是因为数据源地址结构不是省+市+区县+街道格式,若数据源地址结构无明显规律,建议url代码中添加“&city”,运行过程中有啥问题,请私聊我可改进代码~

49a47507a67144382c771565f5651c52.png

R语言

微信号|lxl18094156586

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值