【问题标题】:Can't Import com.google.android.gms.location.LocationServices无法导入 com.google.android.gms.location.LocationServices
【发布时间】:2014-10-11 19:07:38
【问题描述】:

我正在尝试获取最准确的位置。

到目前为止,我已经成功地使用了 LocationClient,就像在 Google 文档中一样: http://developer.android.com/training/location/retrieve-current.html

但现在我发现这个类很旧: http://developer.android.com/reference/com/google/android/gms/location/package-summary.html

“此类已弃用。使用 LocationServices”。

但是,即使在此站点中的 LocationServices 示例中: Android LocationClient class is deprecated but used in documentation

看来我们应该使用导入:com.google.android.gms.location.LocationServices 但是我不能导入这个类..

有什么想法吗?

编辑:

我在此页面中找到: h2ttps://github.com/M66B/XPrivacy/issues/1774

看似类似的问题,推测: “似乎是 Play services 5.0 的一部分。”
我想知道是不是这样 - 有人试过吗?

【问题讨论】:

  • 您确定您使用的是最新的 Play Services 库项目吗?
  • @CommonsWare 根据 SDK 管理器,我使用的是 Rev: 17。它似乎是最新的
  • 这就是你下载的。您是否将特定下载的版本复制到某处,然后将该副本附加到您的应用程序项目中?如果您的应用项目正在使用一些旧版本的库项目,这将解释缺少的类。
  • 修订版 19 是最新的。我不确定它是否有任何区别
  • @CommonsWare 我不记得对我下载的内容进行任何更新。但是,如何检查我的应用实际使用的版本?

标签: android


【解决方案1】:

确保您的 gradle 依赖项中有以下项目:

dependencies {
    implementation 'com.google.android.gms:play-services-location:7.+'
}

【讨论】:

  • compile 现在已弃用。改用实现
【解决方案2】:

请不要使用compile 'com.google.android.gms:play-services:9.0.1'

在 6.5 之前的 Google Play 服务版本中,您必须将整个 API 包编译到您的应用中。在某些情况下,这样做会使您的应用中的方法数量(包括框架 API、库方法和您自己的代码)保持在 65,536 个限制以下变得更加困难。

从 6.5 版开始,您可以有选择地将 Google Play 服务 API 编译到您的应用中。例如,在您的情况下,您只需要这个。

compile 'com.google.android.gms:play-services-maps:9.0.1'
compile 'com.google.android.gms:play-services-location:9.0.1'

要使用 fused api 获取最后一个位置,您必须拥有 compile 'com.google.android.gms:play-services-location:9.0.1'

For more Information

希望对你有帮助

【讨论】:

  • 如果你编译 'com.google.android.gms:play-services:x.x.x 你会得到一个警告信息。使用您实际需要的元素。顺便说一句,compile 现在已经替换为 implementation
  • 但是有一个问题,如果我按照您的要求添加带有“播放服务位置”而不是“播放服务”的版本,我是否应该删除“compile 'com.google.android.gms:play -服务地图:9.0.1'“? (或者这可能对其他东西有用吗?)一句话:你告诉不要使用的库完全弃用了吗?
【解决方案3】:

以下内容可能会有所帮助,

  1. 您应该已下载播放服务最新版本 22。
  2. 在依赖项中添加这一行 编译 'com.google.android.gms:play-services:+'

它应该在项目中导入相应的包。

【讨论】:

  • 最后的“+”是什么意思?它的意思是“给我这个库的最新版本”?
  • 是的,没错。尽管不建议使用“+”,因为它会继续获得任何可用的新 lib 版本,并且某些版本可能会出现您不希望成为应用程序一部分的无意错误。因此,如果您确定版本最好是使用修复版本。
【解决方案4】:

我遇到了同样的问题,并通过在 Android SDK 管理器中将 Google Play 服务从 17 更新到最新(当前为 22)来解决它,如 cmets 中所述。

在 Eclipse 中这样做:

  • 从 Eclipse 中删除 google-play-services_lib 项目
  • 按照Adding SDK Packages 的第 3 步操作。当您启动 Android SDK Manager 时,它应该为 Google Play 服务显示“可用更新:Rev 22”。勾选后点击“安装N个包”进行更新。
  • 关注Setting Up Google Play Services再次导入google-play-services_lib项目。

【讨论】:

  • 这是否意味着您还必须在要启动应用程序的每部手机上安装最新的 Google Play 服务?
  • 是的。通常,该应用会要求您安装最新的 Google Play 服务,如果它比要求的旧。我不确定提示是否由 Google Play 服务自动完成,或者您需要额外的编码才能实现。
  • 这是否也需要在手机中注册一个谷歌帐户?或者没有这样的帐户更新也可以工作?
【解决方案5】:

LocationClient 已弃用。你必须使用GoogleApiclient,像这样:

1:声明一个 GoogleApiClient 变量

private GoogleApiClient mGoogleApiClient;

2:实例化

mGoogleApiClient = new GoogleApiClient.Builder(mThisActivity)
     .addApi(LocationServices.API)
     .addConnectionCallbacks(this)
     .addOnConnectionFailedListener(this)
     .build();

3:实现回调

public class YourClass extends BaseFragment implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener, LocationListener {

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // your code goes here
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        //your code goes here
    }

    @Override
    public void onConnectionSuspended(int cause) {
        //your code goes here       
    }
}

4:开始获取位置更新:

LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);

5:删除位置更新:

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

6:获取最后已知位置:

private Location mCurrentLocation;

mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

【讨论】:

    【解决方案6】:

    截至 2019 年或之后,最佳答案已过时,将不起作用。

    改为添加:

    implementation 'com.google.android.gms:play-services-location:17.0.0'

    dependenciesbuild.gradle

    【讨论】:

      【解决方案7】:

      如果您使用的是 eclipse,请按照以下步骤操作:-

      1. 从 SDK Manger 更新 Google Play 库。
      2. 从工作区中删除旧的 google-play-services_lib 并移除所有依赖项。
      3. 导入更新 google-play-services_lib 并确保选择复制到工作区选项

      1. 添加 google-play-services_lib 以构建项目路径,它应该显示相对路径

      2. 如果步骤不起作用重新启动 eclipse。

      【讨论】:

        【解决方案8】:

        这是一个与here相似的问题:

        在 Android Studio 中:

        1. 右击你的项目“app”文件夹并点击->模块设置

        2. 点击“依赖”标签

        3. 单击 + 号添加新依赖项并选择“库依赖项” 查找您需要的库并添加它

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-20
          • 2017-01-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-31
          相关资源
          最近更新 更多