【问题标题】:Google direction API gives wrong distance and time duration谷歌方向 API 给出了错误的距离和持续时间
【发布时间】:2014-08-01 21:41:50
【问题描述】:

我已经在我的 android APP 中实现了 google map api 功能。 谷歌地图 api 给了我一个正确的路线(方向),但它给出了两个端点之间的错误距离和时间。

我已经在 android 中以 XML 格式实现了这个 google map api。 以下是我用来获取两个端点之间距离的代码。 (我使用的是“驾驶”模式)。

public String getDistanceText(Document doc) {
        NodeList nl1 = doc.getElementsByTagName("distance");
        Node node1 = nl1.item(0);
        NodeList nl2 = node1.getChildNodes();
        Node node2 = nl2.item(getNodeIndex(nl2, "text"));
        Log.i("DistanceText", node2.getTextContent());
        return node2.getTextContent();
    }

    public int getDistanceValue(Document doc) {
        NodeList nl1 = doc.getElementsByTagName("distance");
        Node node1 = nl1.item(0);
        NodeList nl2 = node1.getChildNodes();
        Node node2 = nl2.item(getNodeIndex(nl2, "value"));
        Log.i("DistanceValue", node2.getTextContent());
        return Integer.parseInt(node2.getTextContent());
    } 

以下是获取两个端点之间持续时间的代码:

public String getDurationText(Document doc) {
        NodeList nl1 = doc.getElementsByTagName("duration");
        Node node1 = nl1.item(0);
        NodeList nl2 = node1.getChildNodes();
        Node node2 = nl2.item(getNodeIndex(nl2, "text"));
        Log.i("DurationText", node2.getTextContent());
        return node2.getTextContent();
    }

    public int getDurationValue(Document doc) {
        NodeList nl1 = doc.getElementsByTagName("duration");
        Node node1 = nl1.item(0);
        NodeList nl2 = node1.getChildNodes();
        Node node2 = nl2.item(getNodeIndex(nl2, "value"));
        Log.i("DurationValue", node2.getTextContent());
        return Integer.parseInt(node2.getTextContent());
    }

我在这里做错了什么?

任何帮助将不胜感激。

【问题讨论】:

标签: android google-maps google-directions-api


【解决方案1】:

你读错了节点。当您想要路线的完整持续时间时,您必须阅读最后一个持续时间元素

来自

NodeList nl1 = doc.getElementsByTagName("duration");   
Node node1 = nl1.item(0);
NodeList nl2 = node1.getChildNodes();

NodeList nl1 = doc.getElementsByTagName("duration");
Node node1 = nl1.item(nl1.getLenght()-1);
NodeList nl2 = node1.getChildNodes();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多