0

I am developing application to track user location. In that I need to know how to show shortest direction from one location to another in Map Activity.I should not be a straight line. It should be like a road path.

1

2 Answers 2

4

If you want to draw a polyline between 2 points and following road, you can try with the library Google-Directions-Android

You can add the library on gradle with compile 'com.github.jd-alexander:library:1.0.7'

You can use all your point (Latlng) and use them into the waypoint method.

Routing routing = new Routing.Builder()
                .travelMode(/* Travel Mode */)
                .withListener(/* Listener that delivers routing results.*/)
                .waypoints(/*waypoints*/)
                .build();
    routing.execute();

actual code

 start = new LatLng(18.015365, -77.499382);
    waypoint= new LatLng(18.01455, -77.499333);
    end = new LatLng(18.012590, -77.500659);

    Routing routing = new Routing.Builder()
                .travelMode(Routing.TravelMode.WALKING)
                .withListener(this)
                .waypoints(start, waypoint, end)
                .build();
    routing.execute();
4
  • check updated answer. Check the entire working sample at github.com/jd-alexander/Google-Directions-Android Commented Apr 5, 2016 at 6:12
  • Thanks.. But it doesn't work for me. sample project also not working. Commented Apr 5, 2016 at 7:18
  • After several test I maked it work . Thanks @crashOveride Commented Apr 5, 2016 at 8:45
  • I discovered this library yesterday, and now, Im using it. But.. What are the limits of usage? I didn't read anything about it in github repository. Thanks!
    – Cristian
    Commented Dec 29, 2016 at 11:53
0
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?saddr=%s&daddr=%s",  "Malakwal", "Lahore");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
2
  • Dear @Hamza Asif, could you post some explanation and clarification to the peace of code in your answer?
    – Artem
    Commented Sep 19, 2018 at 20:42
  • be careful in cases that Google Maps is not available, causing a ActivityNotFoundException Commented Sep 19, 2018 at 20:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.