6

In my app I have a button, which opens a Google map with sent coordinates.

 final ImageView view = findViewById(R.id.navigate);
        view.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                        Uri.parse("http://maps.google.com/maps?daddr="+gps1+","+gps2));
                startActivity(intent);
            }
        });

This is working fine, but I want to offer to the user all navigation apps that he has in his device after button click.

So not to automatically open Google Maps, but let the user choose which app to use.

I found this solution, but this is only for Google maps and Waze. As I can't know which and how many navigation apps the user has on his device, I can't hardcode all existing navigation apps.

String url = "waze://?ll=" + latitude + ", " + longitude + "&navigate=yes";
Intent intentWaze.setPackage("com.waze");
Intent intentWaze = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

String uriGoogle = "google.navigation:q=" + latitude + "," + longitude;
Intent intentGoogleNav.setPackage("com.google.android.apps.maps");
Intent intentGoogleNav = new Intent(Intent.ACTION_VIEW, Uri.parse(uriGoogle));

String title = context.getString(R.string.title);
Intent chooserIntent = Intent.createChooser(intentGoogleNav, title);
Intent[] arr = new Intent[1];
arr[0] = intentWaze;
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arr);
context.startActivity(chooserIntent);

So is there any solution, which can offer all navigation apps that the user has?

For example for sharing function it works like this:

 Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT,
                        infox);
                sendIntent.setType("text/plain");
                startActivity(Intent.createChooser(sendIntent, getString(R.string.share)));

This will show many apps with sharing possibility. How to do the same for apps with map?

EDIT: I added this to google and waze code, so now I have listed in chooser: Google map, Waze and an unknown android icon, when I click it, it shows all navigation apps on my device! However only if I click that unnamed icon and it only opens those maps without navigation.

 String urlx = "geo:" + gps1 + ", " + gps2;
                Intent intentOther = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));

I need to show all those app immediately after button click.

Darksymphony
  • 2,155
  • 30
  • 54
  • 3
    Have you tried the [`geo:` common intent](https://developer.android.com/guide/components/intents-common#ViewMap)? – ianhanniballake Sep 03 '19 at 16:14
  • woohoo, I tried geo before but together with google and waze. Now I tried ONLY geo, like this: String urlx = "geo:" + gps1 + ", " + gps2; Intent intent = new Intent(Intent.ACTION_VIEW); and this really shows me all the navigation apps! That's great, but the problem is, it shows only the point on the map, not the navigation path. My original solution for Google and waze showed also the navigation path, the geo shows only the point (according to the coordinates). So this is a very good approach, just to find a solution for navigation route – Darksymphony Sep 03 '19 at 16:29

2 Answers2

1

So as I found out, there is no universal way to force navigation routes on all navigation apps in intent chooser, so I solved it like I mentioned above, just I added a parameter to geo like this:

geo:0,0?q=latitude,longitude

and add to manifest:

  <data android:scheme="geo"/>

and this shows all navigation apps on my device in the chooser, and mostly shows the point on the map with the coordinates, but some apps like Waze also calculates the route immediately, so I think this is a good solution.

Darksymphony
  • 2,155
  • 30
  • 54
1

This is a year later and I was actually trying to use the solution above and I encountered some issues. Such as two Waze apps in the selector

I have just rewritten the solution in kotlin and what actually worked for me

  val url = "waze://?ll=" + latitude + ", " + longitude + "&navigate=yes"
  var intentWaze = Intent(Intent.ACTION_VIEW, Uri.parse(url)).setPackage("com.waze")

  val uriGoogle = "google.navigation:q=" + latitude + "," + longitude
  var intentGoogleNav = Intent(Intent.ACTION_VIEW, Uri.parse(uriGoogle)).setPackage("com.google.android.apps.maps")

  val title: String = context.getString(R.string.app_name)
  val chooserIntent = Intent.createChooser(intentGoogleNav, title)
  val arr = arrayOfNulls<Intent>(1)
  arr[0] = intentWaze
  chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arr)
  context.startActivity(chooserIntent)
George
  • 2,865
  • 6
  • 35
  • 59