我的应用程序需要显示谷歌地图方向从A到B,但我不想把谷歌地图到我的应用程序-相反,我想用一个意图启动它。这可能吗?如果是,怎么做?
当前回答
使用最新的跨平台谷歌地图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(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);
打开华为设备中包含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)
你可以通过这种方式在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方案不会为我工作,地图网页链接没有标签我想出了这个解决方案,这本质上是一个合并的其他解决方案和评论,希望它可以帮助其他人查看这个问题。
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);
推荐文章
- 如何在java中格式化持续时间?(如格式H:MM:SS)
- urlencoder .encode(字符串)已弃用,我应该使用什么代替?
- javax.transaction.Transactional vs . org.springframework.transaction.annotation.Transactional
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 谷歌地图API v3:我可以setZoom后fitBounds?
- 使用Enum实现单例(Java)
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- RabbitMQ与通道和连接之间的关系