0

I want to get the road distance between two geopoint coordinates. Currently I am getting the path but it is not very accurate as it considers the start and end of the road but not the curves in the road. I am using the following code:

Double sLat = Double.parseDouble(getIntent().getStringExtra("sLat"));
Double sLon = Double.parseDouble(getIntent().getStringExtra("sLon"));
Double dLat = Double.parseDouble(getIntent().getStringExtra("dLat"));
Double dLon = Double.parseDouble(getIntent().getStringExtra("dLon"));

MapView mv = (MapView) findViewById(R.id.mapview);
mv.setBuiltInZoomControls(true);
MapController mc = mv.getController();
ArrayList<GeoPoint> all_geo_points = getDirections(sLat, sLon, dLat, dLon);
GeoPoint moveTo = (GeoPoint) all_geo_points.get(0);
mc.animateTo(moveTo);
mc.setZoom(18);
mv.getOverlays().add(new MyOverlay(all_geo_points));

    public static ArrayList<GeoPoint> getDirections(double lat1, double lon1, double lat2, double lon2) 
{
    String url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" +lat1 + "," + lon1  + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric";
    String tag[] = { "lat", "lng" };
    ArrayList<GeoPoint> list_of_geopoints = new ArrayList<GeoPoint>();
    HttpResponse response = null;
    try 
    {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(url);
        response = httpClient.execute(httpPost, localContext);
        InputStream in = response.getEntity().getContent();
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(in);
        if (doc != null) {
            NodeList nl1, nl2;
            nl1 = doc.getElementsByTagName(tag[0]);
            nl2 = doc.getElementsByTagName(tag[1]);
            if (nl1.getLength() > 0) {
                list_of_geopoints = new ArrayList<GeoPoint>();
                for (int i = 0; i < nl1.getLength(); i++) {
                     Node node1 = nl1.item(i);
                     Node node2 = nl2.item(i);
                     double lat = Double.parseDouble(node1.getTextContent());
                     double lng = Double.parseDouble(node2.getTextContent());
                     list_of_geopoints.add(new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)));
                 }
             } else {
                        // No points found
                    }
    }
} 

            catch (Exception e) 
            {
                e.printStackTrace();
            }

            return list_of_geopoints;
        }

        @Override
        protected boolean isRouteDisplayed() {
            // TODO Auto-generated method stub
            return false;
        }

Can anyone give me some ideas how to improve this route and also how to get the distence by road. Currently I am getting crow-fly distance in kilometers but my need is to get distance by road.

Thanks in advance.

Guria
  • 13
  • 2
  • 9

3 Answers3

0

You may want to use The Google Distance Matrix API

For example, here is the distance by car between Paris (France) and Berlin (Germany)

http://maps.googleapis.com/maps/api/distancematrix/json?origins=Paris|France&destinations=Berlin|Germany&mode=car&language=fr-FR&sensor=false

0

If you want to get all the points, you have to parse <polyline><points>a~l~Fjk~uOwHJy@P</points></polyline> and not only take <lat> and <lng>.

Android Maps Utils will help you with parsing that funny string.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • I don't get the point. How to parse a~l~Fjk~uOwHJy@P – Guria Nov 24 '13 at 14:46
  • Well I have found some code to parse points but even then I am having problem as it is not the complete path. It ends in the way just some meters before the destination. any ideas? – Guria Nov 24 '13 at 17:07
  • @Guria There are many such elements in the whole xml file, so parse them all and connect all together. – MaciejGórski Nov 24 '13 at 17:27
0
    <html>
    <head><title>dis</title></head>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?
    sensor=false&libraries=geometry"></script>

    <script>
    var p1 = new google.maps.LatLng(45.463688, 9.18814);
    var p2 = new google.maps.LatLng(46.0438317, 9.75936230000002);

    alert(calcDistance(p1, p2));

    //calculates distance between two points in km's
    function calcDistance(p1, p2) {
      return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
    }

    </script>
    <body>
     </body>
     </html>
master ArSuKa
  • 151
  • 2
  • 14
  • Here's the basic structure, you can later make the coordinates entered dynamic from static, this method is TESTED for displacement and gives answer pop-up in KMs. – master ArSuKa Nov 29 '17 at 07:32