Java 通过Android上的intent启动谷歌地图方向
我的应用程序需要显示谷歌地图从A到B的方向,但我不想把谷歌地图放到我的应用程序中——相反,我想使用Intent启动它。这可能吗?如果是,如何使用?您可以使用以下内容:Java 通过Android上的intent启动谷歌地图方向,java,android,google-maps,android-intent,Java,Android,Google Maps,Android Intent,我的应用程序需要显示谷歌地图从A到B的方向,但我不想把谷歌地图放到我的应用程序中——相反,我想使用Intent启动它。这可能吗?如果是,如何使用?您可以使用以下内容: 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")); startActivi
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:
您也可以在新API中使用Intents。这有点离题,因为您询问了“方向”,但您也可以使用Android文档中描述的Geo URI方案: 使用“地理:纬度,经度”的问题是,谷歌地图只以你的点为中心,没有任何别针或标签 这相当令人困惑,尤其是当你需要指向一个精确的地方或/或询问方向时 如果使用查询参数“geo:lat,lon?q=name”来标记地质点,它将使用查询进行搜索并关闭lat/lon参数 我找到了一种方法,可以使用lat/lon将地图居中,并显示一个带有自定义标签的pin,显示起来非常漂亮,在询问方向或任何其他操作时非常有用:
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(撰写本文时的最新版本)。将忽略坐标并在括号之间搜索具有给定名称的对象。另请参见如果您知道点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);
有关详细信息,请参阅
注意:本例使用的示例文件(截至2013年3月)仍然在线。如果它已脱机,请联机查找kml文件并更改您的url虽然当前的答案很好,但没有一个完全符合我的要求,我只想打开地图应用程序,为每个源位置和目标添加一个名称,使用geo-URI方案对我来说根本不起作用,地图web链接也没有标签,所以我提出了这个解决方案,它本质上是其他解决方案和这里的评论的融合,希望它对其他人查看这个问题有所帮助
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,然后我们可以在没有地图应用程序限制的情况下再次启动活动,我们可以非常肯定的是,我们永远不会在最后干杯,因为互联网浏览器也是启动这个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.setClassName
方法打开内置应用程序Android Maps
Intent i=newintent(Intent.ACTION_视图,Uri.parse(“geo:37.827500,-122.481670”);
i、 setClassName(“com.google.android.apps.maps”,
“com.google.android.maps.MapsActivity”);
星触觉(i);
首先,您需要现在就可以使用隐式意图,android文档为我们提供了非常详细的
为了实现映射意图,您需要创建一个带有两个参数的新意图
- 行动
- Uri
Intent.action\u 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);
第一组坐标是您的起始位置。接下来所有的都是路线点,绘制的路线经过
只需通过在末尾浓缩“/纬度,经度”来继续添加路径点。根据标准,显然有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: 即使谷歌地图应用程序丢失,它也会在浏览器中打开 范例
如果您有兴趣显示当前方向的纬度和经度,可以使用: 方向总是从用户当前位置发出 下面的查询将帮助您执行该操作。您可以在此处传递目标纬度和经度:
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
更多信息请点击此处:
谷歌
方向视图
,源位置为当前位置,目标位置为字符串
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打开谷歌地图:
我们可以使用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”是指自行车
我们可以通过以下方式设置驾驶、步行和自行车模式:
- 驾驶执照
- w步行
- b骑自行车
您可以查看我的原始答案。一个不错的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
})
你可以发射Goo
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
})
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);
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)
}
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)