1

I have implemented the google map api functionality in my android APP. The google map api give me a correct route(Directions), but it gives wrong distance and time between the two endpoint.

I have implemented this google map api in android, in XML format. Following are the code that I have used to get the distance between two endpoint. (I am using the "Driving" mode).

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

Following are the code to get the time duration between two end point:

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

What wrong I did here?

Any help will appreciate.

PTech
  • 163
  • 1
  • 11
  • It is already answered: [Link][1] [1]: http://stackoverflow.com/questions/14394366/find-distance-between-two-points-on-map-using-google-map-api-v2 – whiterabithole Aug 03 '14 at 03:27

1 Answers1

1

You read the wrong node. When you want the complete duration for the route you have to read the last durartion-Element

from

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

to

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