Get driving directions using Google Maps API v2
Asked Answered
W

4

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.

Wasserman answered 24/1, 2013 at 6:10 Comment(1)
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...Aglitter
M
150

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

Matted answered 24/2, 2013 at 16:36 Comment(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.Propylaeum
+1, but for getDurationText() I get values that are way too small. Any help?Penman
Hi, Document doc = builder.parse(in); line is parsing only 1 line of the inputStream for the same code. why?Safety
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 timeDingo
@uofc I added it back for you.Rashidarashidi
My lastest code for Google Direction API github.com/akexorcist/Android-GoogleDirectionAndPlaceLibraryMatted
@Matted I am getting null pointer exception error message by following the codes provided. Do you have any idea?Archfiend
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 supportPothook
@Pothook Update your google play services library to latest versionMatted
@Matted Hi, i'm getting a NullPointerException at nl1 = doc.getElementsByTagName("step"); in your GMapV2Direction class, do you know why?Scribbler
@Fondesa I'm not sure. I guess it doesn't contain with 'step' tag in xml dataMatted
Here is my demo project with API v21: github.com/billypchan/AndroidMapTestThief
Hi everyone, I just updated my latest library. Change XML to JSON and using retrofit, gson and parceler. github.com/akexorcist/Android-GoogleDirectionLibraryMatted
I am getting "REQUEST_DENIED"-Callback, although my key is server key and activated?Endbrain
@GaryKlasen I just updated latest version that you can check error message from API.Matted
Its not drawing path,Any help appreciated.Ankledeep
@Akexorcist... your code is working fine if i have only start and end points.. what should i do to show waypoints in between?? helpBaldric
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, exitingGarges
@AlbertoM What's going on? I just updated my library in 2 months ago.Matted
G
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);
Grounds answered 23/4, 2013 at 4:10 Comment(7)
This is a web implementation I don't think this is what he is going for.Clive
@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?Sycee
@Sycee Can you please post your code? just needed intent part.Grounds
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.Gregggreggory
@Gregggreggory : Convert your lat nd long to string, then replace "," with "." .Convert back into long.Grounds
@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.Beamer
@Sycee just delete the spaces before saddr in the url!Backstairs
A
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);
    }
}
Airman answered 8/10, 2013 at 13:6 Comment(0)
D
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)
        )
Drawtube answered 26/7, 2022 at 10:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.