0

我通过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>

有人可以帮帮我吗?

谢谢你,索菲亚页。

4

2 回答 2

2

尝试

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)
于 2012-08-15T11:44:36.123 回答
0

尝试包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)
于 2018-01-11T05:08:46.813 回答