Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android intent for opening both Waze and Google maps

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

like image 623
Nimrod Nahum Avatar asked Sep 04 '14 10:09

Nimrod Nahum


People also ask

Does Google Maps integrate with Waze?

Despite owning the most popular mapping app in the world, Google acquired Waze for almost a billion dollars in 2013. Google did not shut down the app as many had feared, but it has slowly integrated Waze's crowdsourced driving features into Google Maps.


3 Answers

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

like image 97
Nimrod Nahum Avatar answered Oct 22 '22 12:10

Nimrod Nahum


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);
like image 38
Erich Lotto Avatar answered Oct 22 '22 10:10

Erich Lotto


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();
        }
    }
like image 1
Farrokh Shahriari Avatar answered Oct 22 '22 12:10

Farrokh Shahriari