0

我在latitude和中有两个值longitude。我有一把钥匙。此按钮必须有两个选项才能转到位置信息,Yandex Navi以及Google Maps。当我点击按钮时,我想知道哪一个想要打开它。我怎样才能做到这一点?

4

3 回答 3

1

你可以Intent.createChooser()像这样使用:

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);
于 2018-08-17T11:43:36.717 回答
0

如果它必须是 Google Maps 或 Yandex Navi,最简单的方法可能是确定用户想要使用哪个(通过对话或类似方式),然后将其设置为 Map Intent 的目标。例如,以下是Google 的 Android 文档中的一个意图,它按应用名称定位 Google 地图:

// 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);

这也应该与 Navi 一起使用,方法是将包设置为"ru.yandex.yandexnavi".

但请注意,执行此操作的更标准方法是使用未指定目标应用程序的Map Intent 。这样,您只需提供坐标,然后用户就可以使用他们选择的应用程序:

public void showMap(Uri geoLocation) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(geoLocation);
    if (intent.resolveActivity(getPackageManager()) != null) {
      startActivity(intent);
    }
}
于 2018-08-16T15:31:07.407 回答
0

Andrii Omelchenko 的回答总是为我打开谷歌地图。但是在更改了选择器意图的谷歌和 Yandex 订单后,它在我的情况下有效:

    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..
    }
于 2019-12-15T19:12:20.850 回答