android - Google direction API 给出了错误的距离和持续时间

标签 android google-maps google-directions-api

我已经在我的 android APP 中实现了 google map api 功能。 google map 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());
    }

我这里做错了什么?

任何帮助将不胜感激。

最佳答案

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

来自

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();

关于android - Google direction API 给出了错误的距离和持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25089102/

相关文章:

android - 哪个Android数据库可确保安全性和较小的数据库文件大小?

java - 如何在android中检查时间只在晚上的两个时间之间

html - 谷歌地图信息窗口高度不起作用

java - 如何让 map 标记始终保持在屏幕中央?安卓

google-directions-api - Google 路线 API 限制

android - jBox2d android 绘制动态物体

javascript - 我正在使用 react-native-photo-upload 库上传个人资料图片,但它抛出构建失败错误

google-maps - TurfJs union - 如何忽略内部但 union 略有不同的点?

ios - ios 方向 route 的谷歌地图无法在 swift 中正常工作