【问题标题】:Google Maps API Directions Service return directions in xml formatGoogle Maps API Directions Service 以 xml 格式返回方向
【发布时间】:2015-08-07 10:48:46
【问题描述】:

我已经成功完成了获取路线的过程:

function getRoute(){
    var formato = $('#formato-bizis-select').val();
    var start = $('#input-origen').val();

    $.post("index.php/calcular-ruta", $('#bizis-form').serialize(), function(data){
        var end = data.lat + "," + data.long;
        var request = {
            origin: start,
            destination: end,
            unitSystem: google.maps.UnitSystem.METRIC,
            travelMode: google.maps.TravelMode.DRIVING
        }
        directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }
        });
    });
}

从文档中我知道,您可以从这些不同的 URL 访问 XML 或 JSON 格式的数据:

但是,在 directionService.route() 方法中,我不知道如何在请求中包含 URL 以指示我想要的格式。

【问题讨论】:

标签: javascript json xml google-maps google-maps-api-3


【解决方案1】:

您混淆了两个不同的 API:

  • Directions Service 是 JavaScript Maps API 的一部分,是您在 JavaScript 代码中使用的。
  • Directions API 是一种用于服务器端代码的 HTTP API。

Directions API 确实可以选择将 XML 或 JSON 数据返回到您的服务器端代码。

但是由于您在此处编写 JavaScript,因此您没有使用 Directions API,而是使用了 Directions Service。这不提供 JSON 或 XML 或类似的东西,它为您提供了一个 JavaScript 对象,您可以直接在 JavaScript 代码中使用。

在您的代码中,response 是一个 JavaScript 对象。您可以使用浏览器中的开发者工具直接查看它并查看其属性,您可以编写 JavaScript 代码直接访问它,而无需解析 JSON 或 XML。

【讨论】:

  • 好的,明白了!我也通过 PHP 中的 curl 调用(使用 PHP Slim 框架)成功使用了 Directions API,但是我在将 Directions API 的响应传递给 Javascript 中的 Map 时遇到了麻烦,因为我认为这两个 API 返回了相同的对象.现在我看到他们没有......
  • @daniegarcia254 你是怎么解决这个问题的?我也遇到了同样的问题,请问您有没有将 Directions API 响应转换为 JS 兼容格式?
  • @Madhavan.V 抱歉,我不记得了,但这是项目中受影响的文件,希望对您有所帮助:github.com/daniegarcia254/bizi-meteo/blob/…
猜你喜欢
  • 2016-05-04
  • 1970-01-01
  • 2016-08-07
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
  • 2015-12-12
  • 1970-01-01
  • 2014-01-03
相关资源
最近更新 更多