简体   繁体   English

LocationRequest 和 LocationClient.requestLocationUpdates

[英]LocationRequest and LocationClient.requestLocationUpdates

I have an app that uses Google Maps and the new Fuse Location API.我有一个使用 Google 地图和新的 Fuse Location API 的应用程序。 I use this code to getUpdates when the location changes:当位置更改时,我使用此代码获取更新:

locationClient.requestLocationUpdates(new LocationRequest(),
    new LocationListener() {

    @Override
    public void onLocationChanged(Location location) {
        //Some code
    }
});

I have no problem in my Galaxy Nexus with Android 4.3.我的 Galaxy Nexus 和 Android 4.3 没有问题。 However in my friend's phone (Xperia ST21i) with Android 4.0.4 I can't receive those updates.但是,在我朋友的手机(Xperia ST21i)中,Android 4.0.4 我无法收到这些更新。

I have seen on the Android documentation and on many (if not all) questions on StackOVerflow the use of LocationRequest.create() instead of new LocationRequest() .我已经在 Android 文档和 StackOverflow 上的许多(如果不是全部)问题中看到使用LocationRequest.create()而不是new LocationRequest() This new code still works fine on my phone;这个新代码在我的手机上仍然可以正常工作; I haven't tried it on my friend's phone yet.我还没有在我朋友的手机上试过。

Reading the documentation, my guess is that LocationRequest.create() is just something like:阅读文档,我的猜测是LocationRequest.create()就像:

public static LocationRequest create() { 
    LocationRequest locationRequest= new LocationRequest();
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    locationRequest.setFastestInterval(aNumber);
    locationRequest.setInterval(anotherNumber);
    return locationRequest;
}

What is the difference between this two pieces of code and why it works on my phone, but it doesn't on my friend's.这两段代码有什么区别,为什么它可以在我的手机上工作,但在我朋友的手机上却没有。 Will it work after the change on my friend's phone?在我朋友的手机上更改后它会起作用吗?

If not, what else will I need to do to make it work?如果没有,我还需要做什么才能使它工作?

There's actually an issue in many 4.0.x devices where OnLocationChanged doesn't get called properly if you rely on Network level location.在许多 4.0.x 设备中实际上存在一个问题,如果您依赖网络级别的位置,则无法正确调用OnLocationChanged

Issue report at AOSP Issue Tracker is likely what you're seeing AOSP 问题跟踪器上的问题报告可能就是您所看到的

As stated there, basically the Network level location never refreshes, and since you're using LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY it will try to use the least obtrusive method to give you updates when possible.如上所述,基本上网络级别的位置永远不会刷新,并且由于您使用的是LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY ,它将尝试使用最不显眼的方法在可能的情况下为您提供更新。 I do find it a little bit strange that you still never get updates if GPS and Wifi are available on that device as well.如果 GPS 和 Wifi 在该设备上也可用,您仍然永远不会获得更新,我确实觉得有点奇怪。

A possible way to work around this issue is to change the priority to LocationRequest.PRIORITY_HIGH_ACCURACY .解决此问题的一种可能方法是将优先级更改为LocationRequest.PRIORITY_HIGH_ACCURACY If you believe that solution will be troublesome to your users, you may do something like creating a new LocationRequest with PRIORITY_HIGH_ACCURACY and only using it if your first approach fails.如果您认为该解决方案会给您的用户带来麻烦,您可以执行一些操作,例如使用PRIORITY_HIGH_ACCURACY创建一个新的LocationRequest并且仅在您的第一种方法失败时才使用它。

Particularly, I would set a timer when you first initiate the location request, and use a flag in your LocationListener to indicate whether or not any results are received in that time frame.特别是,当您第一次发起位置请求时,我会设置一个计时器,并在您的LocationListener使用一个标志来指示在该时间范围内是否收到任何结果。 Code examples can be supplied if necessary.如果需要,可以提供代码示例。

I managed to solve my problem.我设法解决了我的问题。 I forgot that being connected (onConnected) doesn't mean that the location is known.我忘记了连接 (onConnected) 并不意味着位置是已知的。 However, I still don't know why my code wasn't working after telling my friend to open the Google Maps App and wait until it starts showing the current location, before opening my app.但是,在告诉我的朋友打开 Google 地图应用程序并等到它开始显示当前位置后,我仍然不知道为什么我的代码不起作用,然后再打开我的应用程序。 Maybe the Google Maps App doesn't use or update directly or indirectly the location of the Google Fusion API.也许 Google Maps App 不会直接或间接使用或更新 Google Fusion API 的位置。

If you want to know how exactly I implemented it, here is a link: MapActivity 如果你想知道我是如何实现它的,这里是一个链接: MapActivity

UPDATE: MapActivity更新: MapActivity

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM