0

We are creating an Android application and we need to calculate exact road distance between two fixed points. We require road distance not the Arial distance(Bird flying distance). We need to calculate the estimate trip cost before the trip starts.

Thanks in Advance. All the answers and helps will be appreciated.

2 Answers2

0

use goolge api

public float getDistance(double lat1, double lon1, double lat2, double lon2) {
        String result_in_kms = "";
        String url = "http://maps.google.com/maps/api/directions/xml?origin=" + lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric";
        String tag[] = {"text"};
        HttpResponse response = null;
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(url);
            response = httpClient.execute(httpPost, localContext);
            InputStream is = response.getEntity().getContent();
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = builder.parse(is);
            if (doc != null) {
                NodeList nl;
                ArrayList args = new ArrayList();
                for (String s : tag) {
                    nl = doc.getElementsByTagName(s);
                    if (nl.getLength() > 0) {
                        Node node = nl.item(nl.getLength() - 1);
                        args.add(node.getTextContent());
                    } else {
                        args.add(" - ");
                    }
                }
                result_in_kms =String.valueOf( args.get(0));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Float f=Float.valueOf(result_in_kms);
        return f*1000;
    }
0

Here is a variation of the first answer. First call getDocument with start and end location, mode (driving, transit, cycling, walking), language. Then pass that document to getTurnByTurn(). This will return an array of 'steps' or legs of a trip, with a distance between the previous step's end point and the new step's end point. Might be in kilometers, need to convert to miles if necessary.

public Document getDocument(String start, String dest, String mode, String language) {

    try {
        start = URLEncoder.encode(start, "utf-8");
        dest = URLEncoder.encode(dest, "utf-8");
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    }

    long milliseconds = System.currentTimeMillis();
    long seconds = milliseconds/1000;

    String url = "https://maps.googleapis.com/maps/api/directions/xml?departure_time="
            + seconds
            + "&origin=" + start
            + "&destination=" + dest
            + "&language=" + language
            + "&sensor=false&mode=" + mode;

    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse response = httpClient.execute(httpPost, localContext);
        InputStream in = response.getEntity().getContent();
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(in);
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

public ArrayList<SearchItem> getTurnByTurn (Document doc) {
    NodeList nl1, nl2, nl3;
    ArrayList<SearchItem> listDirections = new ArrayList<SearchItem>();
    nl1 = doc.getElementsByTagName("step");
    if (nl1.getLength() > 0) {
        for (int i = 0; i < nl1.getLength(); i++) {
            Node node1 = nl1.item(i);
            nl2 = node1.getChildNodes();

            Node distanceNode = nl2.item(getNodeIndex(nl2, "distance"));
            nl3 = distanceNode.getChildNodes();
            Node textNode = nl3.item(getNodeIndex(nl3, "text"));
            String distance = textNode.getTextContent();

            Node durationNode = nl2.item(getNodeIndex(nl2, "duration"));
            nl3 = durationNode.getChildNodes();
            textNode = nl3.item(getNodeIndex(nl3, "text"));
            String duration = textNode.getTextContent();

            Node instructionsNode = nl2.item(getNodeIndex(nl2, "html_instructions"));
            String instructions = instructionsNode.getTextContent();
            String details = distance + " -- " + duration;


            listDirections.add(new SearchItem(instructions, details, "", false));
        }
    }

    return listDirections;
}
Michael Dougan
  • 1,698
  • 1
  • 9
  • 13