Get driving directions using Google Maps API v2

111,455

Solution 1

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

Solution 2

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);

Solution 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);
    }
}
Share:
111,455
Anukool
Author by

Anukool

Updated on July 30, 2022

Comments

  • Anukool
    Anukool almost 2 years

    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.

  • BBonDoo
    BBonDoo about 11 years
    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.
  • yams
    yams about 11 years
    This is a web implementation I don't think this is what he is going for.
  • Buneme Kyakilika
    Buneme Kyakilika almost 11 years
    +1, but for getDurationText() I get values that are way too small. Any help?
  • Zeba
    Zeba almost 11 years
    @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?
  • Basim Sherif
    Basim Sherif almost 11 years
    @Zeba Can you please post your code? just needed intent part.
  • Adnan Amjad
    Adnan Amjad over 10 years
    Hi, Document doc = builder.parse(in); line is parsing only 1 line of the inputStream for the same code. why?
  • Pierre
    Pierre over 10 years
    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
  • bjb568
    bjb568 about 10 years
    @uofc I added it back for you.
  • Akexorcist
    Akexorcist about 10 years
    My lastest code for Google Direction API github.com/akexorcist/Android-GoogleDirectionAndPlaceLibrary
  • Admin
    Admin almost 10 years
    @Akexorcist I am getting null pointer exception error message by following the codes provided. Do you have any idea?
  • JAPS
    JAPS over 9 years
    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.
  • Basim Sherif
    Basim Sherif over 9 years
    @JAPS : Convert your lat nd long to string, then replace "," with "." .Convert back into long.
  • user2273146
    user2273146 over 9 years
    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
  • Akexorcist
    Akexorcist over 9 years
    @user2273146 Update your google play services library to latest version
  • Giorgio Antonioli
    Giorgio Antonioli about 9 years
    @Akexorcist Hi, i'm getting a NullPointerException at nl1 = doc.getElementsByTagName("step"); in your GMapV2Direction class, do you know why?
  • Akexorcist
    Akexorcist about 9 years
    @Fondesa I'm not sure. I guess it doesn't contain with 'step' tag in xml data
  • suri
    suri almost 9 years
    @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.
  • Bill Chan
    Bill Chan over 8 years
    Here is my demo project with API v21: github.com/billypchan/AndroidMapTest
  • Akexorcist
    Akexorcist over 8 years
    Hi everyone, I just updated my latest library. Change XML to JSON and using retrofit, gson and parceler. github.com/akexorcist/Android-GoogleDirectionLibrary
  • Sjd
    Sjd about 8 years
    @Zeba just delete the spaces before saddr in the url!
  • Gary Klasen
    Gary Klasen about 8 years
    I am getting "REQUEST_DENIED"-Callback, although my key is server key and activated?
  • Akexorcist
    Akexorcist about 8 years
    @GaryKlasen I just updated latest version that you can check error message from API.
  • Joe
    Joe almost 8 years
    Its not drawing path,Any help appreciated.
  • Elizabeth
    Elizabeth over 7 years
    @Akexorcist... your code is working fine if i have only start and end points.. what should i do to show waypoints in between?? help
  • Afrig Aminuddin
    Afrig Aminuddin almost 7 years
    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
  • Akexorcist
    Akexorcist over 6 years
    @AlbertoM What's going on? I just updated my library in 2 months ago.