我的应用程序需要显示谷歌地图方向从A到B,但我不想把谷歌地图到我的应用程序-相反,我想用一个意图启动它。这可能吗?如果是,怎么做?


你可以使用这样的代码:

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参数和值。

您可以使用实际的街道地址,而不是经纬度。然而,这将给用户一个对话框,选择通过浏览器打开它或谷歌地图。

这将在导航模式下直接启动谷歌地图:

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

更新

2017年5月,谷歌推出了通用、跨平台谷歌地图url的新API:

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

你也可以在新的API中使用intent。

这有点跑题了,因为你问了“方向”,但你也可以使用Android文档中描述的Geo URI方案:

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

使用“geo:latitude,longitude”的问题是谷歌Maps仅以您的点为中心,没有任何大头针或标签。

这很让人困惑,尤其是当你需要指明一个确切的地方或问路的时候。

如果使用查询参数“geo:lat,lon?”Q =name”为了标记您的地理点,它使用查询来搜索并取消lat/lon参数。

我找到了一种方法,以纬度/lon居中的地图和显示一个自定义标签的大头针,非常好的显示和有用的时候,询问方向或任何其他行动:

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

注意(by @TheNail):在地图v.7中不能工作(撰写本文时的最新版本)。将忽略坐标并搜索圆括号之间具有给定名称的对象。请参见谷歌将7.0.0映射为location的Intent

如果您知道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);

更多信息,请看这个SO答案

注意:本例使用的示例文件(截至mar13)仍然在线。如果它已经脱机,在网上找到一个KML文件并更改您的url

尽管当前的答案是伟大的,没有人做很我在寻找什么,我想打开地图应用,添加一个名称为每个源位置和目的地,使用地理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,因为互联网浏览器也是启动这个url方案的有效应用。

        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

编辑:

对于方向,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);

试试这个

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

你可以尝试通过Intent打开内置应用Android Maps。setClassName方法。

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

首先你需要知道,现在你可以使用隐式意图,android文档为我们提供了一个非常详细的通用意图 为了实现映射意图,你需要创建一个带有两个参数的新意图

行动 尤里

对于动作,我们可以使用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);
    }

对于多个路径点,也可以使用以下方法。

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”来继续添加路径点。根据谷歌文档,显然有23个路径点的限制。不确定这是否也适用于Android。

这对我来说很管用:

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

使用最新的跨平台谷歌地图url: 即使谷歌地图应用程序是缺失的,它将在浏览器中打开

例如https://www.google.com/maps/dir/?api=1&origin=81.23444 67.0000目的地= 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);

如果你想从当前方向显示纬度和经度,你可以使用这个:

方向总是从用户当前位置给出。

下面的查询将帮助您执行此操作。你可以在这里通过目的地的经纬度:

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

更多信息: 谷歌为Android地图意图

谷歌方向视图,源位置作为当前位置,目标位置作为给定的字符串

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是一个字符串变量,可以根据需要进行修改。

使用不同模式的Intent打开谷歌映射:

我们可以打开谷歌地图应用程序使用意图:

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。

你可以在这里找到更多关于谷歌地图的意图。

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

你可以在这里查看我的原始答案。

一个很好的kotlin解决方案,使用lakshman提到的最新跨平台答案:

没有不必要的Uri。toString和Uri。解析一下,这个答案简洁明了:

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

你可以通过这种方式在Android上启动谷歌地图方向

btn_search_route.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String source = et_source.getText().toString();
            String destination = et_destination.getText().toString();

            if (TextUtils.isEmpty(source)) {
                et_source.setError("Enter Soruce point");
            } else if (TextUtils.isEmpty(destination)) {
                et_destination.setError("Enter Destination Point");
            } else {
                String sendstring="http://maps.google.com/maps?saddr=" +
                        source +
                        "&daddr=" +
                        destination;
                Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                        Uri.parse(sendstring));
                startActivity(intent);
            }
        }

    });

试试这个更新意图谷歌地图地址位置找到

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

打开华为设备中包含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)
            Uri uri = Uri.parse("google.navigation:q=" + Latitude + "," + longitude + "&mode=d"); 
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            intent.setPackage("com.google.android.apps.maps");
            startActivity(intent);