Android Studio地图API v2

5

我的代码在Eclipse上运行得非常完美。我在Android Studio上创建了一个新项目并添加了我的类,我还按照这个受欢迎的教程回答 如何在Android Studio中创建使用Google Maps Api v2的Android应用程序?

现在,代码没有显示任何红色标记,并且正常检测导入,但是当我构建项目时,就会出现以下错误: Gradle:报错:找不到符号:com.google.android.gms.maps Gradle:报错:找不到符号:com.google.android.gms.maps.model

顺便说一句,我尝试导入MAPS示例项目,像教程中所说的那样,那也很好,但我不知道为什么我的新项目会给我这些错误,尽管我已经完全按照教程操作。

大家有什么想法吗?


1
@RachelGallen Google Maps Android API v2并未被弃用。Google Maps Javascript API v2已经被弃用。 - MaciejGórski
@MaciejGórski,我的错,我会删除评论! - Rachel Gallen
4个回答

10
如果您创建了一个新项目(使用Gradle),然后遵循教程,那完全是错误的。当使用Gradle(默认情况下新项目都是)时,您不能使用用户界面来添加依赖项。您需要在build.gradle文件中手动添加它们。目前这是一个糟糕的解决方案,我们正在努力改进它。

认为这应该在被接受的答案中分享,因为这是我迄今为止找到的唯一解决方案。 分步指南:http://aetherstudios.net/pages/gradle.html - Dayn Goodbrand
1
修改build.gradle文件的示例将非常棒。看起来您可以从以下答案中复制它。 - Brian White

6

简单来说:

  1. Run Android Studio
  2. Create new project
  3. Create new module of Android Library type, remember to set the package name to com.google.android.gms
  4. Copy res, src folders and AndroidManifest.xml file from google-play-services_lib (you can find it in your SDK folder if you installed it using SDK Manager) to the appropriate folder in your library module ([Project]/[Library]/src/main)
  5. Remove android-support-v4.jar from libs folder under your library module, live only google-play-services.jar and google-play-services.jar.properties files there.
  6. Make sure your build.gradle file in library module contains following:

    dependencies {
        compile files('libs/google-play-services.jar')
    }
    
  7. Make sure your build.gradle for application module contains following:

    dependencies {
        compile files('libs/android-support-v4.jar')
        compile project(':[LibraryModuleName]')
    }
    
  8. Make sure your settings.gradle contain following:

    include ':MapStudio', ':[LibraryModuleName]'
    
  9. You can grab layout and manifest settings from Google Maps API v2 tutorial

  10. Keep in mind that android-support-v4.jar is neccessary only when you want to support Android v2

你可以在我的博客上找到更详细的描述。


我们不允许这样做。但是,您可以[编辑]以提供足够的细节来回答问题,同时仍然指向您相关的博客文章。如果您想这样做,那么之后请标记以使其未被删除。 - user1228

4
我尝试了很多关于这个的教程,但最终找到一个看起来有效的简单解决办法。
我刚刚在我的mac上安装了Android Studio 0.2.3,并采用以下步骤在一个全新的hello world项目模板上查看地图片段:
1) 点击Android Studio工具栏中的SDK管理器按钮。
2)在“Extras”下找到“Google play服务”并安装它。
3)在您的src目录下的build.gradle文件中,将此行添加到依赖项中:
compile 'com.google.android.gms:play-services:3.1.36'

4) 按照以下教程订购并安装您的API密钥:https://developers.google.com/maps/documentation/android/start#the_google_maps_api_key

5) 将碎片添加到您的布局XML中:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

6) 现在,您应该可以在您的设备上运行您的项目。


请注意,如果您支持旧版本,则片段必须是com.google.android.gms.maps.SupportMapFragment - JJD
这个方法在AS 0.3.1下终于对我起作用了,尽管在此之前我不得不重启AS。 - Brian White

-1

对于那些完全被gradle项目中外部库的实现所困惑的人,我有最简单和最可靠的解决方案。

逐步指南:http://aetherstudios.net/pages/gradle.html

这可能需要一些工作,但这是我发现最可靠的完成方法。


欢迎来到SO并感谢您的贡献!这并不是对所提出问题的直接回答。虽然包含支持材料的链接通常很有用,但您应该在此至少给出一些直接的答案或代码示例。链接有时会失效,而在此处提供答案有助于保持连贯性和相关性。 - eaj
尽管我同意这并没有直接回答问题,但提供的链接正是所需,几乎不值得被点踩。如果你想在Android Studio中使用Gradle实现Google Maps,这是我发现的唯一可行的方法。 - Dayn Goodbrand

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接