首页 文章

通过Android上的意图启动Google Map 路线

提问于
浏览
343

我的应用需要显示从A到B的Google Map 路线,但我不想将Google Map 放入我的应用中 - 相反,我想使用Intent启动它 . 这可能吗?如果有,怎么样?

14 回答

  • 114

    虽然目前的答案很棒,但没有一个能达到我想要的效果,我只想打开 Map 应用程序,为每个源位置和目的地添加一个名称,使用geo URI方案对我来说不起作用在所有和 Map 网站链接没有标签所以我想出了这个解决方案,这基本上是在这里做出的其他解决方案和评论的合并,希望它有助于其他人查看这个问题 .

    String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?saddr=%f,%f(%s)&daddr=%f,%f (%s)", sourceLatitude, sourceLongitude, "Home Sweet Home", 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);
    

    要使用当前位置作为起点(遗憾的是我还没有找到标记当前位置的方法),请使用以下内容

    String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?daddr=%f,%f (%s)", 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);
    

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

    String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?daddr=%f,%f (%s)", 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();
                }
            }
    

    附:在我的例子中使用的任何纬度或经度都不能代表我的位置,任何与真实位置的相似之处纯属巧合,也就是说我不是来自非洲:P

    EDIT:

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

    Uri navigationIntentUri = Uri.parse("google.navigation:q=" + 12f +"," + 2f);//creating intent with latlng
    Intent mapIntent = new Intent(Intent.ACTION_VIEW, navigationIntentUri);
    mapIntent.setPackage("com.google.android.apps.maps");
    startActivity(mapIntent);
    
  • 1

    试试这个

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr="+src_lat+","+src_ltg+"&daddr="+des_lat+","+des_ltg));
    intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
    startActivity(intent);
    
  • 21

    使用最新的 cross-platform Google Maps URLs :即使谷歌 Map 应用程序丢失,它也会在浏览器中打开

    示例https://www.google.com/maps/dir/?api=1&origin=81.23444,67.0000&destination=80.252059,13.060604

    Uri.Builder builder = new Uri.Builder();
    builder.scheme("https")
            .authority("www.google.com").appendPath("maps").appendPath("dir").appendPath("").appendQueryParameter("api", "1")
            .appendQueryParameter("destination", 80.00023 + "," + 13.0783);
    String url = builder.build().toString();
    Log.d("Directions", url);
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
    
  • 2

    那么您可以尝试使用 Intent.setClassName 方法打开内置应用程序Android Maps .

    Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("geo:37.827500,-122.481670"));
    i.setClassName("com.google.android.apps.maps",
        "com.google.android.maps.MapsActivity");
    startActivity(i);
    
  • 7

    这有点偏离主题,因为您要求“方向”,但您也可以使用Android文档中描述的Geo URI方案:

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

    使用“geo:latitude,longitude”的问题在于Google Map 仅在您的位置居中,没有任何引脚或标签 .

    这非常令人困惑,特别是如果你需要指向一个精确的地方或/并询问方向 .

    如果使用查询参数“geo:lat,lon?q = name”来标记地理点,它将使用搜索查询并关闭lat / lon参数 .

    我找到了一种用lat / lon中心 Map 的方法,并显示一个带有自定义标签的图钉,非常适合展示,并在询问路线或任何其他动作时非常有用:

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
    Uri.parse("geo:0,0?q=37.423156,-122.084917 (" + name + ")"));
    startActivity(intent);
    

    NOTE (by @TheNail): Not working in Maps v.7 (latest version at the time of writing). Will ignore the coordinates and search for an object with the given name between the parentheses. See also Intent for Google Maps 7.0.0 with location

  • 2

    Google DirectionsView 将源位置作为当前位置和目标位置,以字符串形式给出

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?f=d&daddr="+destinationCityName));
    intent.setComponent(new ComponentName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"));
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
    

    在上面 destinationCityName 是一个字符串变量,根据需要修改它 .

  • 1

    如果您有兴趣从当前方向显示纬度和经度,您可以使用:

    始终从用户当前位置给出方向 .

    以下查询将帮助您执行该操作 . 您可以在此处传递目的地纬度和经度:

    google.navigation:q=latitude,longitude
    

    使用以上作为:

    Uri gmmIntentUri = Uri.parse("google.navigation:q=latitude,longitude");
    Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
    mapIntent.setPackage("com.google.android.apps.maps");
    startActivity(mapIntent);
    

    或者,如果您想通过位置显示,请使用:

    google.navigation:q=a+street+address
    

    更多信息:Google Maps Intents for Android

  • 4

    对于多个航路点,也可以使用以下内容 .

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
        Uri.parse("https://www.google.com/maps/dir/48.8276261,2.3350114/48.8476794,2.340595/48.8550395,2.300022/48.8417122,2.3028844"));
    startActivity(intent);
    

    第一组坐标是您的起始位置 . 所有接下来都是路点,绘制的路线经过 .

    只需通过在结尾处结束"/latitude,longitude"来保持添加方式点 . 根据google docs,显然有23个方向的限制 . 不确定这是否也适用于Android .

  • 0

    如果你知道A点,B点(以及其间的任何特征或轨迹),你可以使用KML文件和你的意图 .

    String kmlWebAddress = "http://www.afischer-online.de/sos/AFTrack/tracks/e1/01.24.Soltau2Wietzendorf.kml";
    String uri = String.format(Locale.ENGLISH, "geo:0,0?q=%s",kmlWebAddress);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    startActivity(intent);
    

    有关更多信息,请参阅this SO answer

    注意:此示例使用的示例文件(截至3月13日)仍处于联机状态 . 如果它已脱机,请在线查找kml文件并更改您的网址

  • 567

    首先你需要现在可以使用隐式意图,android文档为我们提供了一个非常详细的common intents用于实现你需要用两个参数创建一个新意图的 Map 意图

    • 行动

    • Uri

    对于行动,我们可以使用 Intent.ACTION_VIEW ,对于Uri,我们应该构建它,下面我附加了一个示例代码来创建,构建,启动活动 .

    String addressString = "1600 Amphitheatre Parkway, CA";
    
        /*
        Build the uri 
         */
        Uri.Builder builder = new Uri.Builder();
        builder.scheme("geo")
                .path("0,0")
                .query(addressString);
        Uri addressUri = builder.build();
        /*
        Intent to open the map
         */
        Intent intent = new Intent(Intent.ACTION_VIEW, addressUri);
    
        /*
        verify if the devise can launch the map intent
         */
        if (intent.resolveActivity(getPackageManager()) != null) {
           /*
           launch the intent
            */
            startActivity(intent);
        }
    
  • 12
    Uri.Builder directionsBuilder = new Uri.Builder()
            .scheme("https")
            .authority("www.google.com")
            .appendPath("maps")
            .appendPath("dir")
            .appendPath("")
            .appendQueryParameter("api", "1")
            .appendQueryParameter("destination", lat + "," + lon);
    
    startActivity(new Intent(Intent.ACTION_VIEW, directionsBuilder.build()));
    
  • 3

    Open Google Maps using Intent with different Modes:

    我们可以使用意图打开Google Map 应用:

    val gmmIntentUri = Uri.parse("google.navigation:q="+destintationLatitude+","+destintationLongitude + "&mode=b")
    val mapIntent = Intent(Intent.ACTION_VIEW, gmmIntentUri)
    mapIntent.setPackage("com.google.android.apps.maps")
    startActivity(mapIntent)
    

    这里,“mode = b”适用于自行车 .

    我们可以使用以下方式设置驾驶,步行和骑车模式:

    • d用于驾驶

    • w走路

    • b用于骑自行车

    您可以使用Google Map here找到有关意图的更多信息 .

    注意:如果没有自行车/汽车/步行路线,那么它会显示“找不到那里的路”

    你可以查看我的原始答案here .

  • 83

    你可以使用这样的东西:

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
        Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345"));
    startActivity(intent);
    

    要从当前位置开始导航,请删除 saddr 参数和值 .

    您可以使用实际的街道地址而不是纬度和经度 . 但是,这将为用户提供一个对话框,可以选择通过浏览器还是Google打开它 Map .

    这将直接在导航模式下启动Google Map :

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
        Uri.parse("google.navigation:q=an+address+city"));
    

    UPDATE

    2017年5月,Google推出了针对通用跨平台Google Map 网址的新API:

    https://developers.google.com/maps/documentation/urls/guide

    您也可以将Intent与新API一起使用 .

  • 1

    这对我有用:

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("http://maps.google.co.in/maps?q=" + yourAddress));
    if (intent.resolveActivity(getPackageManager()) != null) {
       startActivity(intent);
    }
    

相关问题