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

时间:2010-04-18 14:27:41

标签: java android google-maps android-intent

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

17 个答案:

答案 0 :(得分:593)

您可以使用以下内容:

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地图:

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

<强>更新

2017年5月,Google推出了适用于通用跨平台Google地图网址的新API:

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

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

答案 1 :(得分:116)

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

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

使用“geo:latitude,longitude”的问题是,Google地图只会在您的位置居中,没有任何针脚或标签。

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

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

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

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

注意(由@TheNail提供):不适用于Maps v.7(撰写本文时的最新版本)。将忽略坐标并在括号之间搜索具有给定名称的对象。另请参阅Intent for Google Maps 7.0.0 with location

答案 2 :(得分:92)

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

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

为了完整性,如果用户没有安装地图应用程序,那么捕获ActivityNotFoundException将是一个好主意,然后我们可以在没有地图应用限制的情况下再次启动活动,我们可以非常肯定我们由于互联网浏览器也是启动此网址方案的有效应用程序,因此最终将永远不会进入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.S。 在我的例子中使用的任何纬度或经度都不代表我的位置,任何与真实位置的相似之处纯属巧合,也就是说我不是来自非洲:P

修改

对于路线,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);

答案 3 :(得分:29)

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

答案 4 :(得分:14)

使用最新的跨平台Google地图网址: 即使谷歌地图应用程序丢失,它也会在浏览器中打开

示例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);

答案 5 :(得分:7)

您可以尝试使用Intent.setClassName方法打开内置应用Android地图。

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

答案 6 :(得分:4)

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

  

始终从用户的当前位置提供方向。

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

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

答案 7 :(得分: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);

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

通过联合&#34; /纬度,经度&#34;继续添加方式点在末尾。根据{{​​3}},显然有23个方向点的限制。不确定这是否也适用于Android。

答案 8 :(得分:3)

打开包含 HMS 的华为设备中的地图应用:

const val GOOGLE_MAPS_APP = "com.google.android.apps.maps"
const val HUAWEI_MAPS_APP = "com.huawei.maps.app"

    fun openMap(lat:Double,lon:Double) {
    val packName = if (isHmsOnly(context)) {
        HUAWEI_MAPS_APP
    } else {
        GOOGLE_MAPS_APP
    }

        val uri = Uri.parse("geo:$lat,$lon?q=$lat,$lon")
        val intent = Intent(Intent.ACTION_VIEW, uri)
        intent.setPackage(packName);
        if (intent.resolveActivity(context.packageManager) != null) {
            context.startActivity(intent)
        } else {
            openMapOptions(lat, lon)
        }
}

private fun openMapOptions(lat: Double, lon: Double) {
    val intent = Intent(
        Intent.ACTION_VIEW,
        Uri.parse("geo:$lat,$lon?q=$lat,$lon")
    )
    context.startActivity(intent)
}

HMS 检查:

private fun isHmsAvailable(context: Context?): Boolean {
var isAvailable = false
if (null != context) {
    val result =
        HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(context)
    isAvailable = ConnectionResult.SUCCESS == result
}
return isAvailable}

private fun isGmsAvailable(context: Context?): Boolean {
    var isAvailable = false
    if (null != context) {
        val result: Int = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context)
        isAvailable = com.google.android.gms.common.ConnectionResult.SUCCESS == result
    }
    return isAvailable }

fun isHmsOnly(context: Context?) = isHmsAvailable(context) && !isGmsAvailable(context)

答案 9 :(得分:3)

这对我有用:

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

答案 10 :(得分:2)

试试这个

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

答案 11 :(得分:2)

Google class QueryWithSoftDelete(BaseQuery): def __new__(cls, *args, **kwargs): obj = super(QueryWithSoftDelete, cls).__new__(cls) with_deleted = kwargs.pop('_with_deleted', False) if len(args) > 0: super(QueryWithSoftDelete, obj).__init__(*args, **kwargs) return obj.filter_by(deleted=False) if not with_deleted else obj return obj def __init__(self, *args, **kwargs): pass def with_deleted(self): return self.__class__(db.class_mapper(self._mapper_zero().class_), session=db.session(), _with_deleted=True) ,其中源位置为当前位置,目标位置为字符串

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); } 是一个字符串变量,根据需要修改它。

答案 12 :(得分:2)

使用Intent在不同模式下打开Goog​​le地图:

我们可以使用Intent打开Goog​​le Maps应用:

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地图here的意图的更多信息。

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

您可以查看我的原始答案here

答案 13 :(得分:1)

一个不错的kotlin解决方案,使用了lakshman sai提到的最新跨平台答案...

虽然没有不必要的Uri.toString和Uri.parse,但此答案干净而最小:

 val intentUri = Uri.Builder().apply {
      scheme("https")
      authority("www.google.com")
      appendPath("maps")
      appendPath("dir")
      appendPath("")
      appendQueryParameter("api", "1")
      appendQueryParameter("destination", "${yourLocation.latitude},${yourLocation.longitude}")
 }.build()
 startActivity(Intent(Intent.ACTION_VIEW).apply {
      data = intentUri
 })

答案 14 :(得分:1)

如果你知道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文件并更改您的URL

答案 15 :(得分:0)

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

  • 行动
  • 乌里

我们可以使用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);
    }

答案 16 :(得分:0)

试试这个更新后的Google地图意图地址找出来

 Uri gmmIntentUri1 = Uri.parse("geo:0,0?q=" + Uri.encode(address));
    Intent mapIntent1 = new Intent(Intent.ACTION_VIEW, gmmIntentUri1);
    mapIntent1.setPackage("com.google.android.apps.maps");
    startActivity(mapIntent1);