无法导入 com.google.android.gms.location.LocationServices

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25370598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:23:03  来源:igfitidea点击:

Can't Import com.google.android.gms.location.LocationServices

android

提问by d_e

I'm trying to get the most accurate location.

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

till now I'v used successfully LocationClient as in Google docs: http://developer.android.com/training/location/retrieve-current.html

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

But now I have discovered that this class is old: http://developer.android.com/reference/com/google/android/gms/location/package-summary.html

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

"This class is deprecated. Use LocationServices".

“不推荐使用此类。使用 LocationServices”。

However in the examples of LocationServices even in this site: Android LocationClient class is deprecated but used in documentation

然而,即使在这个站点的 LocationServices 示例中: Android LocationClient 类已被弃用,但在文档中使用

It seem we should use an import: com.google.android.gms.location.LocationServices However I can't import this class..

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

Any Ideas ?

有任何想法吗 ?

Edit:

编辑:

I have found in this page: h2ttps://github.com/M66B/XPrivacy/issues/1774

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

with seemingly similar problem, a speculation that: "Seems to be part of Play services 5.0."
I wonder if that is the case -- has anybody tried this ?

与看似相似的问题,一个猜测说: “似乎是Play services 5.0的一部分。”
我想知道是否是这种情况——有人试过吗?

回答by k.chao.0424

Make sure you have the following item in your gradle dependency:

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

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

回答by Karue Benson Karue

Please Don't use compile 'com.google.android.gms:play-services:9.0.1'

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

In versions of Google Play services prior to 6.5, you had to compile the entire package of APIs into your app. In some cases, doing so made it more difficult to keep the number of methods in your app (including framework APIs, library methods, and your own code) under the 65,536 limit.

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

From version 6.5, you can instead selectively compile Google Play service APIs into your app. For example, in your case you just needed this.

从 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'

To use fused api to get last location you must have 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

想要查询更多的信息

Hope it helps

希望能帮助到你

回答by Vinayak

Below might help,

下面可能有帮助,

  1. You should have play service latest revision 22 downloaded.
  2. Add this line in dependencies compile 'com.google.android.gms:play-services:+'
  1. 您应该下载了播放服务最新修订版 22。
  2. 在依赖项 compile 'com.google.android.gms:play-services:+' 中添加这一行

It should import the respective package in project.

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

回答by Naveen Kumar M

LocationClient is deprecated. You have to use GoogleApiclient, like this:

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

1: Declare a GoogleApiClient variable

1:声明一个GoogleApiClient变量

private GoogleApiClient mGoogleApiClient;

2: Instantiate

2:实例化

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

3: Implement Callback

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: Start to get Location Updates:

4:开始获取位置更新:

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

5: Remove Location Updates:

5:删除位置更新:

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

6: Get Last Known Location:

6:获取最后已知位置:

private Location mCurrentLocation;

mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

回答by Hiroshi Ichikawa

I encountered the same issue, and solved it by updating Google Play Services from 17 to the latest (currently 22) in Android SDK Manager, as mentioned in the comments.

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

To do that in Eclipse:

要在 Eclipse 中做到这一点:

  • Delete google-play-services_lib project from your Eclipse
  • Follow Step 3 of Adding SDK Packages. When you launch Android SDK Manager, it should say "Update available: Rev 22" for Google Play Services. Check it and click "Install N packages" to update it.
  • Follow Setting Up Google Play Servicesto import google-play-services_lib project again.
  • 从 Eclipse 中删除 google-play-services_lib 项目
  • 按照添加 SDK 包的步骤 3 进行操作。当您启动 Android SDK 管理器时,它应该为 Google Play 服务显示“可用更新:Rev 22”。检查它并单击“安装N个包”以更新它。
  • 按照设置 Google Play 服务再次导入 google-play-services_lib 项目。

回答by Dan Bray

As of 2019 or later, the best answer is outdated and will not work.

截至 2019 年或之后,最佳答案已过时且无法使用。

Instead, add:

相反,添加:

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

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

to dependenciesin build.gradle.

dependenciesbuild.gradle

回答by Hitesh Sahu

Follow this steps if you are using eclipse :-

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

  1. Update Google play library from SDK Manger.
  2. Delete old google-play-services_lib from workspace and remove all dependency.
  3. Import update google-play-services_lib and make sure copy to workspace option is selected
  1. 从 SDK Manger 更新 Google Play 库。
  2. 从工作区中删除旧的 google-play-services_lib 并删除所有依赖项。
  3. 导入更新 google-play-services_lib 并确保选择了复制到工作区选项

copy to work space

复制到工作区

  1. Add google-play-services_lib to build path of project it should be showing relative path enter image description here

  2. If step it is not working restart eclipse .

  1. 添加 google-play-services_lib 以构建项目的路径,它应该显示相对路径 在此处输入图片说明

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

回答by ransh

It's a similar question to issue and answer given here:

这是一个类似的问题,在这里给出和回答:

In Android Studio:

在 Android Studio 中:

  1. Right click on your projects "app" folder and click on -> module settings

  2. Click on the "dependencies" tab

  3. Click on the + sign to add a new dependency and select "Library Dependency" Look for the library you need and add it

  1. 右键单击您的项目“app”文件夹,然后单击 -> 模块设置

  2. 单击“依赖项”选项卡

  3. 点击+号添加新的依赖,选择“Library Dependency”查找你需要的库并添加