0

I have two values in latitude and longitude. I have a key. This button must have two options to go to location information,Yandex Navi and Google Maps. When I click on the button, I want to know which one would like to open it. How can I do that?

fatih
  • 67
  • 1
  • 12
  • Is it supposed to link to both apps even if the user doesn't have them installed? Also, should the user be able to use a different map application if they have one installed? – Milo P Aug 16 '18 at 15:05
  • @MiloP Only these two applications can be used. There will be no other application. If one of these two apps is installed on your phone, it will open the location within the app. – fatih Aug 16 '18 at 15:10

3 Answers3

1

You can use Intent.createChooser() like:

String url = "yandexmaps://maps.yandex.ru/?pt=" + latitude + "" + longitude + "&z=12&l=map";
Intent intentYandex = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intentYandex.setPackage("ru.yandex.yandexmaps");

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

String title = "Select";
Intent chooserIntent = Intent.createChooser(intentGoogle, title);
Intent[] arr = new Intent[1];
arr[0] = intentYandex;
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arr);
startActivity(chooserIntent);
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
  • @fatih At first tale a look at [this](https://developer.android.com/training/basics/intents/sending). Than read something like [this](https://trivedihardik.wordpress.com/2013/10/29/app-chooser/) or [that](https://androidlad.blogspot.com/2015/06/custom-sharing-intent-android-with.html). – Andrii Omelchenko Aug 17 '18 at 12:55
1

Andrii Omelchenko's answer always opens Google Maps for me. But after change google and Yandex order for chooserIntent, it is worked in my case:

    val uriYandex = "yandexnavi://build_route_on_map?lat_to=${latitude}&lon_to=${longitude}"
    val intentYandex = Intent(Intent.ACTION_VIEW, Uri.parse(uriYandex))
    intentYandex.setPackage("ru.yandex.yandexnavi")

    val uriGoogle = Uri.parse("google.navigation:q=${latitude},${longitude}&mode=w")
    val intentGoogle = Intent(Intent.ACTION_VIEW, uriGoogle)
    intentGoogle.setPackage("com.google.android.apps.maps")

    val chooserIntent = Intent.createChooser(intentYandex, title)
    val arr = arrayOfNulls<Intent>(1)
    arr[0] = intentGoogle
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arr)

    val activities = packageManager.queryIntentActivities(chooserIntent, 0)
    if(activities.size>0){
        startActivity(chooserIntent)
    }else{
        //do sth..
    }
kiroglue
  • 341
  • 2
  • 7
0

If it absolutely has to be either Google Maps or Yandex Navi, the easiest way would probably be to figure out which one the user wants to use (via a dialogue or similar), and then set that as the target of a Map Intent. For example, here's an intent from Google's Android documentation that targets Google Maps by app name:

// Creates an Intent that will load a map of San Francisco
Uri gmmIntentUri = Uri.parse("geo:37.7749,-122.4194");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, 
gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

This should also work with Navi by setting the package to "ru.yandex.yandexnavi".

Note, though, that a more standard way to do this would be with a Map Intent that doesn't specify the targeted app. That way, all you would have to provide is the coordinates, and then the user could use their app of choice:

public void showMap(Uri geoLocation) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(geoLocation);
    if (intent.resolveActivity(getPackageManager()) != null) {
      startActivity(intent);
    }
}
Milo P
  • 1,377
  • 2
  • 31
  • 40
  • Thanks for this help me to open it with Google Mapping. But how do I bring an app selection screen and how do I add yandex navi? – fatih Aug 17 '18 at 07:24