3

I try to invoke the navigation from my applicatoin.

I call with this query:

String link = "geo:" + posInfo.getLatitude() + "," + posInfo.getLongitude();
Intent navigateIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
Intent chooser = Intent.createChooser(navigateIntent, ctx.getString(R.string.navigate_intent_chooser_title));
ctx.startActivity(chooser);

The intent chooser displays 2 apps: GoogleMaps and Waze.

The navigation with Waze works perfectly but with Google maps it doesn't work. I get the map displayed but no navigation instructions. If I use GoogleMaps directly then I can navigate so it is something to do with how the intent parameters are passed.

I read in some other answers that I need to use another query for google maps:

"google.navigation:q.."

I have 2 questions:

  1. Do I need different query to different navigation apps?
  2. If 1 is true, how can I use intentChooser with different query?
slashnick
  • 26,167
  • 10
  • 55
  • 67
blay
  • 464
  • 9
  • 23

1 Answers1

5

It's my solution:

private static final String PERFIX_DEFAULT_NAVIGATE_LINK_BY_COORDINATES = "geo:";
private static final String PERFIX_DEFAULT_NAVIGATE_LINK_BY_ADDRESS = "geo:0,0?q=";
private static final String PERFIX_DEFAULT_GOOGLE_NAVIGATE = "google.navigation:q=";

private static Intent getChooserNavigateIntent(Context context, Intent navigateIntent, POSEntityInfo posInfo, boolean isCoordinates) {

    // Check if there is a default app opener for this type of content.
    final PackageManager packageManager = context.getPackageManager();
    ResolveInfo defaultAppInfo = packageManager.resolveActivity(navigateIntent,
            PackageManager.MATCH_DEFAULT_ONLY);


    // create the intent for intent chooser
    List<Intent> targetedOpenIntents = new ArrayList<Intent>();
    List<ResolveInfo> appInfoList = packageManager.queryIntentActivities(navigateIntent,
            PackageManager.MATCH_DEFAULT_ONLY);

    for (ResolveInfo appInfo : appInfoList) {
        String packageName = appInfo.activityInfo.packageName;
        Intent targetedOpenIntent = new Intent(android.content.Intent.ACTION_VIEW).setPackage(packageName);
        targetedOpenIntent = getNavigateIntent(appInfo, targetedOpenIntent, isCoordinates, posInfo);
        targetedOpenIntents.add(targetedOpenIntent);
    }

    // create the intent chooser. delete the first member in the list(the default activity)
    Intent chooserIntent = Intent.createChooser(targetedOpenIntents.remove(0),
            context.getString(R.string.navigate_intent_chooser_title)).putExtra(Intent.EXTRA_INITIAL_INTENTS,
            targetedOpenIntents.toArray(new Parcelable[]{}));

    return chooserIntent;
}

private static Intent getNavigateIntent(ResolveInfo appInfo, Intent navigateIntent, boolean isCoordinates, POSEntityInfo posInfo) {
    // link urls
    final String GOOGLE_NAVIGATE_PACKAGE = "com.google.android.apps.maps";
    String suffixAddressLink = posInfo.getStreet() + " " + posInfo.getStreetNum() + "," + posInfo.getCity();
    String suffixCoordinatesLink = posInfo.getLatitude() + "," + posInfo.getLongitude();

    boolean isGoogleMaps = GOOGLE_NAVIGATE_PACKAGE.equals(appInfo.activityInfo.packageName);

    //build the link url
    StringBuilder link = new StringBuilder();
    link.append(isCoordinates ? (isGoogleMaps ? PERFIX_DEFAULT_GOOGLE_NAVIGATE : PERFIX_DEFAULT_NAVIGATE_LINK_BY_COORDINATES) : (isGoogleMaps ? PERFIX_DEFAULT_GOOGLE_NAVIGATE : PERFIX_DEFAULT_NAVIGATE_LINK_BY_ADDRESS));
    link.append(isCoordinates ? suffixCoordinatesLink : suffixAddressLink);

    Log.d(TAG, "Link = " + link);

    return navigateIntent.setData(Uri.parse(link.toString()));
}

//Just closed the tags with >

Cesar
  • 35
  • 1
  • 7
blay
  • 464
  • 9
  • 23
  • what is PreferenceAccessor, how do i actually use this ? – Lena Bru Mar 02 '14 at 10:08
  • PreferenceAccessor is my class that save prefernces in my device. you dont need use this class. – blay Mar 02 '14 at 14:11
  • ..therefore this isn't actually helpful to anybody but yourself. I can't help but notice that it seems like the only answers you ever mark as accepted are your own and those answers aren't necessarily helpful to anybody but yourself. I'd encourage some more friendly behaviour as part of the *community*. – pc-pdx May 08 '15 at 04:31
  • 1
    @pc-pdx ,I think that can help to every one that need to use google maps and another navigation app – blay Feb 14 '16 at 13:39