[英]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,下面可能有帮助,
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 中做到这一点:
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,请按照以下步骤操作:-
It's a similar question to issue and answer given here :这是一个类似的问题, 在这里给出和回答:
In Android Studio:在 Android Studio 中:
Right click on your projects "app" folder and click on -> module settings右键单击您的项目“app”文件夹,然后单击 -> 模块设置
Click on the "dependencies" tab单击“依赖项”选项卡
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.