如何从我的应用程序中打开标准Google Map应用程序?

LA_*_*LA_ 131 android google-maps

一旦用户在我的应用程序中按下按钮,我想打开标准的Google Map应用程序并显示特定的位置.我该怎么做?(不使用com.google.android.maps.MapView)

Mic*_*ael 231

您应该Intent使用geo-URI 创建一个对象:

String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
context.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

如果要指定地址,则应使用另一种形式的geo-URI : geo:0,0?q=address.

参考:https://developer.android.com/guide/components/intents-common.html#Maps

  • 这会让我移动到这个位置,但它不会在那里放一个气球.我会喜欢一个气球,所以用户可以点击它来获取方向等. (19认同)
  • 不要乱用String.format()进行简单的字符串连接.该方法仅适用于UI文本,这就是为什么小数点表示可能会有所不同.只需使用"+"运算符或StringBuilder:String uri ="geo:"+ lastLocation.getLatitude()+","+ lastLocation.getLongitude(). (5认同)
  • 对于路线,google.navigation现在支持导航意图:q =纬度,经度:Uri gmmIntentUri = Uri.parse("google.navigation:q ="+ 12f"+","+ 2f); Intent mapIntent = new Intent(Intent.ACTION_VIEW,gmmIntentUri); mapIntent.setPackage("com.google.android.apps.maps"); startActivity(mapIntent); (3认同)
  • 好的,我发现了这个问题.`String.format("geo:%f,%f",纬度,经度)`用逗号返回字符串:`geo:59,915494,30,409456`. (2认同)

Dav*_*son 98

您也可以使用http://maps.google.com/maps作为您的URI

String uri = "http://maps.google.com/maps?saddr=" + sourceLatitude + "," + sourceLongitude + "&daddr=" + destinationLatitude + "," + destinationLongitude;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

或者您可以确保仅使用Google地图应用,这样可以阻止意图过滤器(对话框)显示,方法是使用

intent.setPackage("com.google.android.apps.maps");
Run Code Online (Sandbox Code Playgroud)

像这样:

String uri = "http://maps.google.com/maps?saddr=" + sourceLatitude + "," + sourceLongitude + "&daddr=" + destinationLatitude + "," + destinationLongitude;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

或者您可以通过在每组坐标后面的括号内添加一个字符串来为位置添加标签,如下所示:

String uri = "http://maps.google.com/maps?saddr=" + sourceLatitude + "," + sourceLongitude + "(" + "Home Sweet Home" + ")&daddr=" + destinationLatitude + "," + destinationLongitude + " (" + "Where the party is at" + ")";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

要使用用户当前位置作为起点(遗憾的是我还没有找到标记当前位置的方法),请saddr按以下方式删除参数:

String uri = "http://maps.google.com/maps?daddr=" + destinationLatitude + "," + destinationLongitude + " (" + "Where the party is at" + ")";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

为了完整性,如果用户没有安装地图应用程序,那么捕获ActivityNotFoundException是一个好主意,就像@TonyQ所述,那么我们可以在没有地图应用限制的情况下再次启动活动,我们可以非常肯定因为互联网浏览器也是启动这个网址方案的有效应用程序,所以我们最终永远不会到达Toast.

        String uri = "http://maps.google.com/maps?daddr=" + 12f + "," + 2f + " (" + "Where the party is at" + ")";
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
        intent.setPackage("com.google.android.apps.maps");
        try
        {
            startActivity(intent);
        }
        catch(ActivityNotFoundException ex)
        {
            try
            {
                Intent unrestrictedIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                startActivity(unrestrictedIntent);
            }
            catch(ActivityNotFoundException innerEx)
            {
                Toast.makeText(this, "Please install a maps application", Toast.LENGTH_LONG).show();
            }
        }
Run Code Online (Sandbox Code Playgroud)

编辑:

对于路线,google.navigation现在支持导航意图

Uri navigationIntentUri = Uri.parse("google.navigation:q=" + 12f + "," + 2f);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, navigationIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
Run Code Online (Sandbox Code Playgroud)


Dav*_*oho 40

使用String格式会有所帮助,但您必须完全关注语言环境.在德国浮动将用逗号分隔而不是一个点.

String.format("geo:%f,%f",5.1,2.1);在语言环境中使用英语,结果将是,"geo:5.1,2.1"但您将获得德语区域设置"geo:5,1,2,1"

您应该使用英语区域设置来防止此行为.

String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
context.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

要将标签设置为地理位置,您可以使用以下方法扩展地理位置:

!但要小心,geo-uri仍在开发中 http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00

String uri = String.format(Locale.ENGLISH, "geo:%f,%f?z=%d&q=%f,%f (%s)", 
                           latitude, longitude, zoom, latitude, longitude, label);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
context.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)


Sni*_*las 8

从谷歌检查此页面:

http://developer.android.com/guide/appendix/g-app-intents.html

您可以使用表单的URI

geo:latitude,longitude
Run Code Online (Sandbox Code Playgroud)

打开Goog​​le地图查看器并将其指向某个位置.


M. *_*han 8

要前往带有 PIN 码的位置,请使用:

String uri = "http://maps.google.com/maps?q=loc:" + destinationLatitude + "," + destinationLongitude;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

对于没有 pin 的情况,请在 uri 中使用:

 String uri = "geo:" + destinationLatitude + "," + destinationLongitude;
Run Code Online (Sandbox Code Playgroud)


Ton*_*nyQ 6

有时如果没有任何与geo:protocal相关联的应用程序,您可以使用try-catch来获取ActivityNotFoundException来处理它.

当您使用默认情况下未安装谷歌地图的androVM等仿真器时会发生这种情况.


小智 6

您还可以使用下面的代码段,这样就可以在意图开始之前检查谷歌地图的存在.

Uri gmmIntentUri = Uri.parse(String.format(Locale.ENGLISH,"geo:%f,%f", latitude, longitude));
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(mapIntent);
}
Run Code Online (Sandbox Code Playgroud)

参考:https://developers.google.com/maps/documentation/android-api/intents