google api距离矩阵转换为R

我已经通过google api服务计算了城市之间的距离。 我需要的距离成中的R的距离矩阵,但是当我使用的代码:google api距离矩阵转换为R

de1 <- "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Berlin|Hamburg|Muenchen|Koeln|Frankfurt am Mein|Duesseldorf|Bremen|Hannover|Nuernberg&destinations=Berlin|Hamburg|Muenchen|Koeln|Frankfurt am Mein|Duesseldorf|Bremen|Hannover|Nuernberg&mode=driving&language=en&sensor=false" 

webpage1 <- getURL(de1)

webpage1 <- readLines(tc <- textConnection(webpage1)); close(tc)

pagetree <- htmlTreeParse(webpage1, error=function(...){})

如果我然后输入“pagetree”我得到一个错误信息。

> pagetree 

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<meta name="viewport" content="initial-scale=1, minimum-scale=1, width=device-width">

<title>Error 400 (Bad Request)!!1</title>

<style>

*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}

</style>

<a href="//www.google.com/"><img src="//www.google.com/images/errors/logo_sm.gif" alt="Google"></a>

</head>

<body>

<p><b>400.</b> <ins>That&acirc;&#128;&#153;s an error.</ins>

</p>

<p>Your client has issued a malformed or illegal request. <ins>That&acirc;&#128;&#153;s all we know.</ins></p>

</body>

</html>

有人可以帮我吗?

谢谢, sofia p。

回答:

尝试

require(RCurl) 

webpage1 <- getURL(URLencode(de1))

也返回的数据为JSON格式

require(RJSONIO) 

data<-fromJSON(webpage1)

也许有用

,如果你想xml返回尝试

require(XML) 

de2 <- "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Berlin|Hamburg|Muenchen|Koeln|Frankfurt am Mein|Duesseldorf|Bremen|Hannover|Nuernberg&destinations=Berlin|Hamburg|Muenchen|Koeln|Frankfurt am Mein|Duesseldorf|Bremen|Hannover|Nuernberg&mode=driving&language=en&sensor=false"

webpage2 <- getURL(URLencode(de2))

data2<-xmlParse(webpage2)

回答:

试包ggmap

library(ggmap) 

mapdist(from, to, mode = c("driving", "walking", "bicycling"),

output = c("simple", "all"), messaging = FALSE, sensor = FALSE,

language = "en-EN", override_limit = FALSE)

以上是 google api距离矩阵转换为R 的全部内容, 来源链接: utcz.com/qa/262667.html

回到顶部