Question

I know it is possible to get direction between coordinates using Google's direction web service. But is there any other way to get equally accurate route?

I found few questions on SO those use http://maps.google.com/ to get direction. But then also found few answers on other questions which state that it is no longer supported.

I am confused as this is the first time I am dealing with Google maps for android.

Is there any built in android sdk method to get direction?

Was it helpful?

Solution

you can send an intent to the google maps app and have the app do all the work for you but its not documented and probably never will be.

to do that you can either give it a lattitue/longitude like this

Intent NavIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" +latitude +","+longitude));
startActivity(NavIntent);

or using the same intent but with an address and see if maps can resolve the address. (I dont have an example with an address).

Other than using google directions API or other 3rd party direction web services there really is no other way to get directions in your app unless you look at Open Street Maps where you calculate the directions yourself using way points but that is quite involved

OTHER TIPS

Firing an intent to the google maps app is what I've choosen to use in my application, the philosophy here is that Android apps should exploit and complement each other functionnlities

Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse(getDirectionUrl(srcLat, srcLng, dstLat, dstLng)));
if (isGoogleMapsInstalled(this)) {
    i.setComponent(new ComponentName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"));
}
startActivity(i);

method to build the directions URL:

public static String getDirectionUrl (double srcLat, double srcLng, double dstLat, double dstLng) {
    //return google map url with directions 
    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.google.com/maps?f=d&saddr=")
    .append(srcLat)
    .append(",")
    .append(srcLng)
    .append("&daddr=")
    .append(dstLat)
    .append(",")
    .append(dstLng);
    return urlString.toString();          
}

Method to test if Maps is installed:

public static boolean isGoogleMapsInstalled(Context c)  {
        try
        {
            @SuppressWarnings("unused")
            ApplicationInfo info = c.getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0 );
            return true;
        } 
        catch(PackageManager.NameNotFoundException e)
        {
            return false;
        }
    }

The downside is that Google may change the URL structure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top