Android intent for opening both Waze and Google maps
Asked Answered
T

4

20

There are a few similar posts but I couldn't find an exact one. basically, I want to open both Google maps and Waze with the same intent. At first I tried this:

final String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);

Waze navigated directly to the right location and Google maps opened the right place. then I realized that Google maps doesn't put a pin on the location so it's hard for the user to know where it is exactly. So I looked around and realized that Google maps requires the "?q=..(label)" for that... I changed the uri construction to:

final String uri = String.format(Locale.ENGLISH, "geo:%f,%f?q=%f,%f (%s)", latitude, longitude, latitude, longitude, name);

But then Waze did 2 things: navigated to the right place AND run a search on the label. This required the user to click the back button to close the search results screen and remain with the navigation to the right place.

I looked everywhere for an answer but failed to find a solution that will achieve both. At first I thought that it's not possible and Waze has a bug... but then I noticed that Facebook messenger is doing exactly what I want. when clicking on a message with a location it will open both apps: Google maps will have a pin (with a label) and Waze will navigate straight to that location without running a search.

Few questions about the above: 1. (Of course) How can I achieve that? 2. How can I know how the Facebook messenger's intent being built? (Can I catch it in anyway) 3. What's the reason behind having the label only with the "?q=.."?

Thanks

Touchline answered 4/9, 2014 at 10:14 Comment(1)
can you post the code that asks the user to choose between Waze and Google Maps?Hose
T
21

Never mind. I was able to intercept the Facebook messenger with the below app and figured that the URI should be as follows:

String.format(Locale.ENGLISH, "geo:0,0?q=") + android.net.Uri.encode(String.format("%s@%f,%f", label, latitude, longitude), "UTF-8");

The app: https://play.google.com/store/apps/details?id=uk.co.ashtonbrsc.android.intentintercept&feature=search_result#?t=W251bGwsMSwyLDEsInVrLmNvLmFzaHRvbmJyc2MuYW5kcm9pZC5pbnRlbnRpbnRlcmNlcHQiXQ..

Thanks

Touchline answered 4/9, 2014 at 16:40 Comment(1)
This only works if the label holds the address :( Only if you remove the label Waze will navigate and only if you have the label google maps will navigateOshea
C
4

Following Nimrod's tip I've installed the app and intercepted the intent from whatsapp's location feature. Here's the full Intent tested on maps and waze:

String uri = "http://maps.google.com/maps?q=loc:"+latitude+","+longitude+" ("+label+")";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
intent.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
intent.setData(Uri.parse(uri));
startActivity(intent);
Crain answered 31/3, 2016 at 19:17 Comment(0)
D
2

I had the same problem - wanted the navigation app (whatever it is) to show a pin (preferably with a custom label) and be ready to navigate the user there. However, having a label in the query resulted in some apps running a search or (worse) assuming the closest address match to the label and showing that as the destination (ignoring the coordinates). So, to achieve a reliable behaviour I had to forget having a named label and ended up with the following:

final String geoIntentData = "geo:" + latitude + "," + longitude + "?q=" + latitude + "," + longitude;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(geoIntentData));

So far it works in all apps I tested (Google Maps, Waze, Sygic, Moovit and even Komoot and Windy Maps). Please let me know if you try this solution and it doesn't work in another app!

Denti answered 24/2, 2022 at 11:43 Comment(3)
Best solution, for waze label is not a problem. Can you remember which apps we need to remove label ?Piercing
Sorry, it was a long time ago (for my brain).Denti
sure, i've tried no problem !Piercing
M
1

My final solution for opening both Waze & GoogleMap applications to get direction ( works like a charm ) :

 String uri = "";
    Intent intent;
    try {

        uri = String.format(Locale.ENGLISH,"geo:" + location.getLat() + "," +location.getLng() + "?q=" + location.getLat()+","+location.getLng()+" ("+location.getName()+")");
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    } catch (ActivityNotFoundException ex) {
        try {
            Intent unrestrictedIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
            startActivity(unrestrictedIntent);
        } catch (ActivityNotFoundException innerEx) {
            getSnackbar(getResources().getString(R.string.map_install_application), Snackbar.LENGTH_LONG).show();
        }
    }
Meill answered 1/5, 2018 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.