LocationClient不会在模拟器更新getLastLocation() [英] LocationClient does not update getLastLocation() on emulator

查看:180
本文介绍了LocationClient不会在模拟器更新getLastLocation()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是谷歌播放服务API来获取我的应用程序位置更新。然而,由于某种原因,client.getLastLocation()不当客户端被连接改变其值。只有在通过注册监听器的位置变化请求位置更新。我想这可能是一个仿真器唯一的问题,但即使当我手动更改使用DDMS或Telnet,getLastLocation值的位置()依然不改。

I'm using the google play services api to get location updates in my app. However, for some reason, client.getLastLocation() doesn't changes its value when a client is connected. Only after requesting location updates by registering a listener will the location change. I think it may be an emulator only problem, but even when I manually change the location using ddms or telnet, the value of getLastLocation() still doesn't change.

不幸的是,我的手机没有出现在亚行,所以我不能测试它在实际设备上,但没有任何人有任何想法,为什么会发生这种情况?这不是什么大不了的事,因为我可以通过请求位置更新解决这个问题,但我想提供一个设置,允许用户手动更新,而不是让它每隔几秒钟,上传自己的位置。有些code,以显示我在说什么。

Unfortunately, my phone isn't showing up in adb, so I can't test it on an actual device, but does anyone have any ideas as to why this would happen? It's not that big of a deal because I can solve it by requesting location updates, but I would like to provide a setting to allow users to manually update their location instead of having it uploaded every few seconds. Some code to show what I'm talking about.

@Override
public void onClick(View v) {
    Location newLocation = client.getLastLocation();
    if(newLocation == null)
    {
        Toast.makeText(getActivity(), "Finding location", Toast.LENGTH_SHORT).show();
        return;
    }
    if(distanceSignificant(currentLocation, newLocation))
    {
        currentLocation = newLocation;
        Log.d(TAG, "distance significant, updating location");
            uploadNewLocation();
        }
    }
});

我每次点击该按钮,该位置将是相同的这是什么最后一次。

Every time I click that button, the location will be the same as what it was last.

@Override
public void onConnected(Bundle data) {
    Log.d(TAG, "connected to locations");
    Toast.makeText(getActivity(), "Starting location services", Toast.LENGTH_SHORT).show();
    currentLocation = client.getLastLocation();

    if(currentLocation == null)
    {
        Log.d(TAG, "can't add user location yet, not available");
    }

    if(shouldUpdate)
    {
        client.requestLocationUpdates(locationRequest, this);
        updateButton.setVisibility(View.GONE);
    }
    else
    {
        updateButton.setVisibility(View.VISIBLE);
    }

}

在这种情况下,shouldUpdate在onCreateView设置,并通过用户设置来设置。

In this case, shouldUpdate is set in onCreateView and is set by a user setting.

有谁知道我应该做些什么来解决这个问题?我觉得我失去了一些东西简单。

Does anyone know what I should do to fix this? I feel like I'm missing something simple.

推荐答案

这是因为 getLastLocation 是一个非阻塞的,亲电池调用来获取最后的已知位置(这可能是旧的值),当没有积极获取位置更新也不会神奇地改变。

This happens because getLastLocation is a non-blocking, pro-battery call to get the last known location (this might be old value) and when nothing is actively retrieving location updates it won't be magically changed.

您可以做的就是在用户请求的位置开始请求位置更新和停止它,当你得到的第一个位置。最好的事情

The best thing you can do to get location on user request is starting requesting location updates and stopping it when you get the first location.

这篇关于LocationClient不会在模拟器更新getLastLocation()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆