Get the distance between two locations in android?

前端 未结 10 574
醉梦人生
醉梦人生 2020-11-28 07:57

i need to get distance between two location, but i need to get distance like blue line in the picture. \"picure\"

10条回答
  •  醉话见心
    2020-11-28 08:47

    Use This:

    private String getDistanceOnRoad(double latitude, double longitude,
                double prelatitute, double prelongitude) {
            String result_in_kms = "";
            String url = "http://maps.google.com/maps/api/directions/xml?origin="
                    + latitude + "," + longitude + "&destination=" + prelatitute
                    + "," + prelongitude + "&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.format("%s", args.get(0));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result_in_kms;
        }
    

提交回复
热议问题