同步获取Android上的最后一个已知位置 [英] Get the last known location on Android synchronously

查看:145
本文介绍了同步获取Android上的最后一个已知位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用LocationClient(v2 API)以同步方式在Android上获取最后一个已知位置的正确"方法是什么?

What would be the "right" way to get the last known location on Android using LocationClient (v2 API) in a synchronous manner?

更新

这是我想出的最好的方法(它不是同步的,但是克服了每次需要最后一个已知位置时处理connect()onConnected()的负担):

This is the best I've come up with (it's not synchronous but it overcomes the burden of dealing with connect() and onConnected() each time the last known location is needed):

public enum SystemServicesNew implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {

    INSTANCE;

    private LocationClient mLocationClient;
    private Location mLastKnownLocation;

    static {
        INSTANCE.mLocationClient = new LocationClient(MyApp.getAppContext(), INSTANCE, INSTANCE);
        INSTANCE.mLastKnownLocation = new Location("");
        INSTANCE.mLastKnownLocation.setLatitude(0);
        INSTANCE.mLastKnownLocation.setLongitude(0);
        INSTANCE.getLastKnownLocation(); // fire it already so subsequent calls get the real location
    }

    public Location getLastKnownLocation()
    {
        if(!mLocationClient.isConnected()) {
            mLocationClient.connect();
            return mLastKnownLocation;
        }
        mLastKnownLocation = mLocationClient.getLastLocation();
        return mLastKnownLocation;
    }

    @Override
    public void onConnected(Bundle bundle) {
        Toast.makeText(MyApp.getAppContext(), "LocationClient:Connected", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDisconnected() {
        Toast.makeText(MyApp.getAppContext(), "LocationClient:Disconnected", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Toast.makeText(MyApp.getAppContext(), connectionResult.toString(), Toast.LENGTH_SHORT).show();
    }
}

我的Java技能...欠发达...有什么改进建议吗?

My Java skills are... less developed... any improvement suggestions?

推荐答案

执行此操作的合法方法是使用LocationClient的

A legitimate way of doing this is to use LocationClient's

getLastLocation()

getLastLocation()

如果要以同步方式进行操作,则应记住,它不是精确的位置,在极少数情况下甚至可能不可用.查看以下文档:

If you want to do it in a synchronous manner, you should keep in mind that it will not be a precise location, and in rare cases it may even be unavailable. Check the documentation below:

public Location getLastLocation ()

返回当前可用的最佳最新位置.

Returns the best most recent location currently available.

如果位置不可用(这种情况很少发生),则将返回null.在尊重位置权限的情况下,将返回最佳精度.

If a location is not available, which should happen very rarely, null will be returned. The best accuracy available while respecting the location permissions will be returned.

此方法提供了一种简化的获取位置的方法.它特别适合于不需要精确位置并且不想为位置更新保留额外逻辑的应用程序.

This method provides a simplified way to get location. It is particularly well suited for applications that do not require an accurate location and that do not want to maintain extra logic for location updates.

这篇关于同步获取Android上的最后一个已知位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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