java - 启动外部 Activity 以显示 map

标签 java android google-maps android-activity maps

这是怎么回事。我想启动一个外部 Activity 来显示地理坐标,而不是在我的应用程序中实现 Google map 。目前我手头有太多东西,我不想经历实现 map 的麻烦。因此,是否可以将参数传递给外部 map Activity ,类似于我们对短信、电话、相机等所做的?

非常感谢!

最佳答案

你可以用这个命令来做到这一点:

private void openMapGeo(String latitude, String longitude) {
    Intent intent2 = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:" + latitude + ","
            + longitude + "?z=17&q=" + latitude + "," + longitude));
    // z stands for zoom level
    // replace the q with a search string works too. Example:
    // intent2 = new Intent(Intent.ACTION_VIEW,
    // Uri.parse("geo:0,0?q=Tokyo"));
    startActivity(intent2);
}

另一种方法是使用 map 网站 url。这将询问用户是否要打开 map 应用程序(如果已安装)或浏览器:

private void openMapUrl(String latitude, String longitude) {
    Intent intent = new Intent(Intent.ACTION_VIEW,
            Uri.parse("http://maps.google.com/?q=" + latitude + "," + longitude));
    startActivity(intent);
}

此处还有一些其他示例:https://github.com/pboos/GoogleMapExamples (看看里面的 Presentation.pdf 文件)


编辑:

如果安装了 google maps 应用程序,要使用以上两个功能,请执行此操作(以上两个功能也需要存在):

private void openMap(String latitude, String longitude) {
    try {
       ApplicationInfo info = getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0 );
        openMapGeo(latitude, longitude);
    } catch(PackageManager.NameNotFoundException e) {
        openMapUrl(latitude, longitude);
    }
}

关于java - 启动外部 Activity 以显示 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9187646/

相关文章:

java - Spring Boot中函数参数的Concat @Query值

java - Google Play 控制台堆栈跟踪

api - 如何在 Google Maps Directions api 中获取所有公交站?

css - 巨大的谷歌地图控件(可能是错误?)

android - 在 Android 的 KML 文件中设置地标的填充颜色

java - 启动时启动java进程并在死机时自动重启

java - JCheckBox 在选择时不更新

java - 如何读取 KieSession 外部插入 KieSession 内部的事实(例如规则结果)?

android - 当应用程序处于前台时,不会调用 onMessageReceived()

android - 重置后无法选择先前选择的 RadioButton