我已经在我的 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/