android - 如何在 Android Studio 中创建使用 Google Maps Api v2 的 Android 应用程序?

标签 android google-maps android-studio google-play-services

我在 Android Studio 中尝试使用 GoogleMap 创建应用时遇到了很多麻烦。

我之前遵循以下指南,使用 Eclipse 作为我的 IDE(几乎)没有问题:

https://developers.google.com/maps/documentation/android/start

我以前从未使用过 Android Studio,而且我在整个项目/模块范式方面都遇到了困难。

我未能成功配置 Google Play 服务 SDK http://developer.android.com/google/play-services/setup.html

这是我遇到的奇怪错误之一:

Gradle: 
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':MyMapApp:compileDebug'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

最佳答案

最后我设法使用 Android Studio 运行 GoogleMapsAPIv2 项目。

编辑:正如 Xavier 所提到的,这种方法仅适用于非基于 gradle 的项目。本教程中使用的 UI 部分将从 AndroidStudio 中排除。所以如果你有自己的项目使用 Gradle 构建系统,你需要手动修改 build.gradle 配置文件,因为 Android Studio UI 不会影响它。

EDIT2:随着AndroidStudio v0.1.1版本的发布,负责模块依赖的UI部分已经被淘汰,所以现在我们需要通过build.gradle文件手动更新依赖。用于更改 gradle 依赖项的 UI 将在下一个版本中发布

EDIT3:对于那些仍在尝试使用这种方法的人 - 请注意它已经过时并且不再起作用了

这是我所做的:

1) 我拿了maps Google Play 服务示例中的项目并将其复制到单独的目录中。这将是我们将尝试运行的 MapsApiV2 项目。在我的 Mac 上,它位于 <sdk_location>/extras/google/google_play_services/samples 我把它放到~/Work/stack/

2) 已复制 google-play-services_lib项目目录到同一个地方( ~/Work/stack ),所以我的工作目录看起来像这样。顺便说一句,lib 项目位于 <sdk_location>/extras/google/google_play_services/libproject : enter image description here

3) 现在让我们打开 Android Studio。在欢迎屏幕上按 Import Project并导入我们的maps项目来自 ~/Work/stack/maps .现在我们看到很多关于未知引用 GMS 库的投诉:

enter image description here

4) 现在我们需要添加 Google Play Service 作为引用库。前往 View -> Open Module Settings

5) 在 Modules 上选项卡,单击 +按钮并选择Import Module并导入您的 GooglePlayServices 库。我没有更改向导中的任何内容,因此单击了Next一直到最后:

enter image description here

6) 现在你需要引用这个导入的库。再次打开此屏幕(转到 View -> Module Settings )。确保您拥有 maps项目和Dependency选项卡被选中。点击+添加依赖项并选择 Library .在那里选择您导入的库:

enter image description here

7) 现在我们可以看到它没有在提示 GMS库,但仍然提示支持库:

enter image description here

8) 让我们修复它。我的支持库位于 <sdk location>/extras/android/support/v13/android-support-v13.jar .因此,让我们尝试将其添加到我们的工作区。转至 View -> Open Module Settings并选择 Libraries标签。选择 + -> Java并选择支持库:

enter image description here

9) 现在它会询问您要将这个库添加到哪个项目,所以请确保您选择了 maps项目:

enter image description here

10) 此时代码应该可以编译没有问题。只需确保您在 Manifest 中定位正确的 SDK 版本即可。

玩得开心

关于android - 如何在 Android Studio 中创建使用 Google Maps Api v2 的 Android 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16596715/

相关文章:

android - 从 Android 中的 Activity 启动服务时出现 "Unable to start service Intent"错误

Android runOnUiThread 不是异步的

android - 如何检测 map 何时完成调整大小

java - "java: package org.apache.cordova does not exist"在Android Studio中编译Cordova时

android - 将图像转换为 JSON

java - Bluestacks Android 模拟器是使用 .Net 构建的?

javascript - 如何在谷歌地图API中给出本地主机的推荐网址?

react-native - react 原生 map : Custom Markers cause extreme lag and slow down on android

android - 如何在我自己的android库模块中添加crosswalk webview?

java - Android Studio AndroidManifest.xml 与 build.gradle