简体   繁体   English

从 JavaScript 获取,解析 XML 有问题? 还是地图 API 错误?

[英]GET from JavaScript, problems parsing XML? Or Maps API errors?

I'm trying to make an HTTP get from Javascript, calling Google Maps to get transit duration from A to B, and then parsing the resulting XML as follows:我正在尝试从 Javascript 获取 HTTP,调用 Google Maps 以获取从 A 到 B 的传输持续时间,然后解析生成的 XML,如下所示:

function getTransitTime(match) {
    var homeBase = '<Address 1>';
    var distURL = 'https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&key=API_KEY_I_GOT&mode=transit&arrival_time=1519977600&origins=' + homeBase + '&destinations=' + match;

    parser = new DOMParser();
    xmlDoc = parser.parseFromString(httpGet( distURL ), "text/xml");
    var finRet = $(xmlDoc).find('duration').text()
    return finRet;
}

function httpGet(theUrl)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, true );
    xmlHttp.send( null );
    return xmlHttp.responseText;
}

I receive both the NoApiKeys and SensorNotRequired errors in the js console.我在 js 控制台中收到 NoApiKeys 和 SensorNotRequired 错误。 What am I doing wrong?我究竟做错了什么?

There are probably even more problems with this:这可能还有更多问题:

  • I'm not sure how to deal with spaces in address 1 (and even worse, "match").我不确定如何处理地址 1 中的空格(更糟糕的是,“匹配”)。

  • Duration has two sub-fields (one being text), not sure if parsing is correct. Duration 有两个子字段(一个是文本),不确定解析是否正确。 Note that my query works correctly in the browser (with the same API key) and the output XML is pasted below:请注意,我的查询在浏览器中正常工作(使用相同的 API 密钥)并且输出 XML 粘贴在下面:

<DistanceMatrixResponse>
    <status>OK</status>
    <origin_address>New York, NY, USA</origin_address>
    <destination_address>Philadelphia, PA, USA</destination_address>
    <row>
        <element>
            <status>OK</status>
            <duration>
                <value>5100</value>
                <text>1 hour 25 mins</text>
            </duration>
            <distance>
                <value>145447</value>
                <text>145 km</text>
            </distance>
        </element>
    </row>
</DistanceMatrixResponse>

Any help?有什么帮助吗? I wandered off trying to make something "simple" work, but ended up in the deep end :|我徘徊试图做一些“简单”的工作,但最终陷入了深渊:|

I've managed to fix this:我设法解决了这个问题:

First, get permissions to make cross-origin requests to the maps API, by including the following in the extension's manifest.json:首先,通过在扩展的 manifest.json 中包含以下内容,获得向地图 API 发出跨域请求的权限:

"permissions": ["https://maps.googleapis.com/"] 

Next, change:接下来,更改:

var finRet = $(xmlDoc).find('duration').text()

to

var finRet = $(xmlDoc).find('duration').find('text').text()

This works because the returned XML has a text tag nested inside the duration tag.这是有效的,因为返回的 XML 有一个嵌套在持续时间标记内的文本标记。

Lastly, the spaces in the URL for the maps call don't really make any difference.最后,maps 调用的 URL 中的空格实际上没有任何区别。 Thanks for everyone for reading and contributing.感谢大家的阅读和贡献。 I'm still looking for a cleaner solution if there's one.如果有的话,我仍在寻找更清洁的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM