我的应用程序需要显示谷歌地图方向从A到B,但我不想把谷歌地图到我的应用程序-相反,我想用一个意图启动它。这可能吗?如果是,怎么做?
当前回答
这对我来说很管用:
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);
}
其他回答
使用不同模式的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。
你可以在这里找到更多关于谷歌地图的意图。
注意:如果没有自行车/汽车/步行的路线,那么它会显示“找不到路”。
你可以在这里查看我的原始答案。
如果您知道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
谷歌方向视图,源位置作为当前位置,目标位置作为给定的字符串
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是一个字符串变量,可以根据需要进行修改。
首先你需要知道,现在你可以使用隐式意图,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(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://maps.google.co.in/maps?q=" + yourAddress));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
推荐文章
- 如何在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与通道和连接之间的关系