0
点赞
收藏
分享

微信扫一扫

R爬取经纬度对应位置信息

booksmg2014 2022-08-05 阅读 94


根据给定的GPS数据,爬取对应的位置信息

执行代码

Get_areadata_by_GPS(location=c("34.59979,119.1974"),ak="yourappkey")

执行结果

R爬取经纬度对应位置信息_数据

函数脚本

######---------Find province and city by GPS---------######
# 参考网址【lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding】
# location<-c("34.59979,119.1974")
#传入经纬度坐标,格式"纬度,经度"
# longitude 经度
# latitude 纬度
# 传入参数[gps、百度秘钥]
Get_areadata_by_GPS(location=c("34.59979,119.1974"),ak="yourappkey")
# require(jsonlite)
Get_areadata_by_GPS <- function(location, ak)
tryCatch(expr = {
if(is.na(location)|grepl("NA",location)){
GPS_data <-
data.frame(
GPS_location = "No_Data",
GPS_province = "No_Data",
GPS_city = "No_Data",
GPS_county = "No_Data",
stringsAsFactors = FALSE
)
}else{
par <- list(
"location" = location,
"output" = "json",
#输出格式为json或者xml
"pois" = 1,
'ak' = ak
)
par <- paste(names(par), par, sep = '=', collapse = '&')
url_pre <- 'http://api.map.baidu.com/geocoder/v2/?'
url <- paste0(url_pre, par)
info <- jsonlite::fromJSON(url)
if (info$status == 0) {
province <- info$result$addressComponent$province
city <- info$result$addressComponent$city
county <- info$result$addressComponent$district
GPS_data <-
data.frame(
GPS_location = location,
GPS_province = province,
GPS_city = city,
GPS_county = county,
stringsAsFactors = FALSE
)
}else{
GPS_data <-
data.frame(
GPS_location = paste0("Error",info$status),
GPS_province = paste0("Error",info$status),
GPS_city = paste0("Error",info$status),
GPS_county = paste0("Error",info$status),
stringsAsFactors = FALSE
)
}

}
},
error=function(e){
GPS_data <<-
data.frame(
GPS_location = "Error",
GPS_province = "Error",
GPS_city = "Error",
GPS_county = "Error",
stringsAsFactors = FALSE
)
})
return(GPS_data)
}

后记

最初还是在TC时,毛老师教给我的~看来还是那时候学的东西多一点。
连绵三天的小雨,心情要晴朗呀~
2017-10-16 于杭州


举报

相关推荐

0 条评论