Suppressing Google Maps Intent Selection Dialog

后端 未结 3 1210
我寻月下人不归
我寻月下人不归 2020-12-14 04:41

When I use this code

   string url = \"somegooglemapsurl.com\";
   Intent mapLauncherIntent = new Intent(android.content.Intent.ACTION_VIEW,  Uri.parse(url))         


        
相关标签:
3条回答
  • 2020-12-14 05:27

    Here are the different methods I found so far:

    1. Directly start the navigation:

    This explicit intent will directly launch Google Navigation:

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
    Uri.parse("google.navigation:q=my+street+address");
    startActivity(intent);
    

    This will directly start the navigation (unfortunately without giving the user a choice of transportation means)

    .

    2. Let the user select the transportation means:

    This will launch the Google navigation app, but lets the user select the transportation means before starting the navigation:

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
    Uri.parse("http://maps.google.com/maps/?daddr=my+street+address");
    intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
    startActivity(intent);
    

    .

    3. Start on the Google map:

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
    Uri.parse("geo:0,0?q=my+street+address");
    intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
    startActivity(intent);
    

    This will launch Maps, from where the user can start the navigation (and select the transportation mode).

    In all cases, you should either use PackageManager and queryIntentActivities() or exception handling to handle cases where the user does not have Google Maps/Navigation installed.

    In my app I use method 2 which works just fine. Hope this helps.

    Add-on: Here's a method to check whether an app is installed or not. I use this to check if "com.google.android.apps.maps" is installed before calling intent.setClassName().

    public static boolean isAppInstalled(String uri) {
        PackageManager pm = getContext().getPackageManager();
        boolean app_installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            app_installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            app_installed = false;
        }
        return app_installed;
    }
    
    0 讨论(0)
  • 2020-12-14 05:28

    First of all, I'll just point out that getting rid of that selection dialog is not necessarily a good thing to do. You want to give the user as much choice in how they handle data as possible, that's part of the beauty of the Android ecosystem. That said, it isn't fair to judge without understanding your application, so...

    You can use the geo protocol:

    http://developer.android.com/guide/appendix/g-app-intents.html

    Instead of passing something like maps.google.com/blablabla format your URI like thisgeo:0,0?q=my+street+address.

    Example:

    Intent mapLauncherIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=1111+imaginary+dr"));
    
    0 讨论(0)
  • 2020-12-14 05:32

    I haven't found a perfect solution, but this will at least open maps with the correct destination and public transportation pre-selected. Then all the user has to do is hit the directions button.

    It also checks if google maps is installed and prefers to use that if so.

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?daddr=0,0%20(Imaginary%20Place)&dirflg=r"));
    if (isAppInstalled("com.google.android.apps.maps")) {
        intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
    }
    startActivity(intent);
    

     

    // helper function to check if Maps is installed
    private boolean isAppInstalled(String uri) {
        PackageManager pm = getApplicationContext().getPackageManager();
        boolean app_installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            app_installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            app_installed = false;
        }
        return app_installed;
    }
    
    0 讨论(0)
提交回复
热议问题