简体   繁体   English

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

[英]Can't Import com.google.android.gms.location.LocationServices

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."与看似相似的问题,一个猜测说: “似乎是Play服务5.0的一部分。”
I wonder if that is the case -- has anybody tried this ?我想知道是否是这种情况——有人试过吗?

Make sure you have the following item in your gradle dependency:确保您的 gradle 依赖项中有以下项目:

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

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

From version 6.5, you can instead selectively compile Google Play service APIs into your app.从 6.5 版开始,您可以有选择地将 Google Play 服务 API 编译到您的应用程序中。 For example, in your case you just needed this.例如,在您的情况下,您只需要这个。

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希望能帮助到你

Below might help,下面可能有帮助,

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

It should import the respective package in project.它应该在项目中导入相应的包。

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

LocationClient is deprecated. LocationClient 已弃用。 You have to use GoogleApiclient , like this:您必须使用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);

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'

to dependencies in build.gradle .build.gradle dependencies build.gradle

Follow this steps if you are using eclipse :-如果您使用 eclipse,请按照以下步骤操作:-

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

复制到工作区

  1. Add google-play-services_lib to build path of project it should be showing relative path添加 google-play-services_lib 以构建项目的路径,它应该显示相对路径在此处输入图片说明

  2. If step it is not working restart eclipse .如果步骤不起作用,请重新启动 eclipse 。

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右键单击您的项目“app”文件夹,然后单击 -> 模块设置

  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点击+号添加新的依赖,选择“Library Dependency”查找你需要的库并添加

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导入com.google.android.gms.location.LocationServices,无法更新位置服务 - Import com.google.android.gms.location.LocationServices, can't update location services 为什么Android Studio无法解析“import com.google.android.gms.location.LocationRequest;”? - Why Android Studio can't resolve “import com.google.android.gms.location.LocationRequest;”? LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this); 尝试投射为com.google.android.gms.location.LocationListener) - LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); Tries to cast as com.google.android.gms.location.LocationListener) Android Studio无法解析导入com.google.android.gms.location.places.Place; - Android studio can not resolve import com.google.android.gms.location.places.Place; 如何导入com.google.android.gms.location ;? - How to import com.google.android.gms.location;? 无法解析导入com.google.android.gms.location.LocationClient - The import com.google.android.gms.location.LocationClient cannot be resolved 找不到com.google.android.gms.location.LocationClient类(android) - Can't find the class com.google.android.gms.location.LocationClient (android) 为什么无法解析导入com.google.android.gms.plus? - Why can't the import com.google.android.gms.plus be resolved? 在'com.google.android.gms:play-services-location:9.4.0'无法导入导入com.google.android.gms.location.places.Places - In 'com.google.android.gms:play-services-location:9.4.0' Unable to to import import com.google.android.gms.location.places.Places 找不到'com.google.android.gms.iid.InstanceIDListenerService' - Can't find 'com.google.android.gms.iid.InstanceIDListenerService'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM