55

I am trying to get the driving direction between the two positions:

LatLng(12.917745600000000000,77.623788300000000000)
LatLng(12.842056800000000000,7.663096499999940000)

The code which i have tried:

Polyline line = mMap.addPolyline(new PolylineOptions().
    add(new LatLng(12.917745600000000000,77.623788300000000000),
    new LatLng(12.842056800000000000,7.663096499999940000))
       .width(5).color(Color.RED));

But this draws a straight line between the two points .

Is there any other method/way to get the driving directions between these two points.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Anukool
  • 5,301
  • 8
  • 29
  • 41
  • 23
    Side note: You can lose some of the digits at the end of the coordinates. You have specified the positions down to 1/10000000000 of a millimeter, which makes the coordinates wrong really fast, considering that the continental drift is in the range of 1/100000 millimeters per second... – Guffa Jan 24 '13 at 06:26

4 Answers4

150

I just release my latest library for Google Maps Direction API on Android https://github.com/akexorcist/Android-GoogleDirectionLibrary

Akexorcist
  • 2,287
  • 1
  • 16
  • 19
  • please let me know more information about your "Sample Code" written on above. In my case, I can't get the polyline, just but a simple map. – BBonDoo Feb 25 '13 at 03:28
  • +1, but for getDurationText() I get values that are way too small. Any help? – Buneme Kyakilika May 01 '13 at 18:19
  • Hi, Document doc = builder.parse(in); line is parsing only 1 line of the inputStream for the same code. why? – Adnan Amjad Oct 21 '13 at 12:36
  • What would work really well is if you made this to insert multiple locations and it finds the quickest path through those locations and navigates you through those. This way making it faster to visit multiple locations saving you fuel and time – Pierre Nov 04 '13 at 18:24
  • @uofc I added it back for you. – bjb568 Apr 05 '14 at 05:32
  • 2
    My lastest code for Google Direction API https://github.com/akexorcist/Android-GoogleDirectionAndPlaceLibrary – Akexorcist Apr 14 '14 at 15:35
  • 3
    @Akexorcist I am getting null pointer exception error message by following the codes provided. Do you have any idea? –  Jul 26 '14 at 13:20
  • When i import this library i get this error (The method setFlat(boolean) is undefined for the type Marker) any idea what i need to do to make it support – user2273146 Nov 01 '14 at 14:59
  • @user2273146 Update your google play services library to latest version – Akexorcist Nov 11 '14 at 08:08
  • 2
    @Akexorcist Hi, i'm getting a NullPointerException at nl1 = doc.getElementsByTagName("step"); in your GMapV2Direction class, do you know why? – Giorgio Antonioli Feb 01 '15 at 14:10
  • @Fondesa I'm not sure. I guess it doesn't contain with 'step' tag in xml data – Akexorcist Feb 18 '15 at 08:44
  • Here is my demo project with API v21: https://github.com/billypchan/AndroidMapTest – Bill Chan Nov 23 '15 at 21:40
  • Hi everyone, I just updated my latest library. Change XML to JSON and using retrofit, gson and parceler. https://github.com/akexorcist/Android-GoogleDirectionLibrary – Akexorcist Dec 06 '15 at 10:34
  • I am getting "REQUEST_DENIED"-Callback, although my key is server key and activated? – Gary Klasen Feb 18 '16 at 07:42
  • @GaryKlasen I just updated latest version that you can check error message from API. – Akexorcist Mar 09 '16 at 22:35
  • Its not drawing path,Any help appreciated. – Joe Jul 14 '16 at 13:42
  • @Akexorcist... your code is working fine if i have only start and end points.. what should i do to show waypoints in between?? help – Elizabeth Dec 30 '16 at 05:56
  • I got an error D:\***\AndroidManifest.xml:56:9-58:47 Error: Element meta-data#com.google.android.geo.API_KEY at AndroidManifest.xml:56:9-58:47 duplicated with element declared at AndroidManifest.xml:30:9-32:71 D:\***\AndroidManifest.xml Error: Validation failed, exiting – Afrig Aminuddin May 03 '17 at 04:55
  • 1
    @AlbertoM What's going on? I just updated my library in 2 months ago. – Akexorcist Jan 23 '18 at 04:40
17

This is what I am using,

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("http://maps.google.com/maps?saddr="+latitude_cur+","+longitude_cur+"&daddr="+latitude+","+longitude));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_LAUNCHER );     
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
Maveňツ
  • 1
  • 12
  • 50
  • 89
Basim Sherif
  • 5,384
  • 7
  • 48
  • 90
  • 2
    This is a web implementation I don't think this is what he is going for. – yams Apr 30 '13 at 13:52
  • @Basim Sherif I tried this out but when it opens up the maps application the start point is always considered as the current location of device even though I provide a different location in 'saddr'. Can you please help me how do I avoid this? – Zeba Jul 26 '13 at 14:23
  • @Zeba Can you please post your code? just needed intent part. – Basim Sherif Jul 29 '13 at 04:37
  • My device locale is Germany when I show the route in Google maps it shows latitude longitude in German units(',' instead of '.').Can We change that?? I need it in English language. – JAPS Oct 09 '14 at 13:30
  • @JAPS : Convert your lat nd long to string, then replace "," with "." .Convert back into long. – Basim Sherif Oct 09 '14 at 13:34
  • @Basim Sherif : simple and effective solution ,this is great help.i would like to follow you for more android help .share your fb id please. – suri May 02 '15 at 06:03
  • 1
    @Zeba just delete the spaces before saddr in the url! – Sjd Feb 03 '16 at 13:00
3

You can also try the following project that aims to help use that api. It's here:https://github.com/MathiasSeguy-Android2EE/GDirectionsApiUtils

How it works, definitly simply:

public class MainActivity extends ActionBarActivity implements DCACallBack{
/**
 * Get the Google Direction between mDevice location and the touched location using the     Walk
 * @param point
 */
private void getDirections(LatLng point) {
     GDirectionsApiUtils.getDirection(this, mDeviceLatlong, point,     GDirectionsApiUtils.MODE_WALKING);
}

/*
 * The callback
 * When the direction is built from the google server and parsed, this method is called and give you the expected direction
 */
@Override
public void onDirectionLoaded(List<GDirection> directions) {        
    // Display the direction or use the DirectionsApiUtils
    for(GDirection direction:directions) {
        Log.e("MainActivity", "onDirectionLoaded : Draw GDirections Called with path " + directions);
        GDirectionsApiUtils.drawGDirection(direction, mMap);
    }
}
0

I've found this -

val polyline1 = map?.addPolyline(
            PolylineOptions().clickable(true)
                .add(LatLng(23.8103, 90.4125), LatLng(4.0383, 21.7587))
                .geodesic(true)
        )
mefahimrahman
  • 197
  • 2
  • 6
  • 29