在Android中获取最后一个位置



我遵循了本教程http://developer.android.com/training/location/retrieve-current.html检索我的设备的位置,但它不起作用。

当我这样做时:

mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

"mLastLocation"为空,为什么?

编辑

我试着像Niru的回答一样:

public Location setLocation()
    {
        Location location;
        final int MIN_TIME_BW_UPDATES = 300;
        final int MIN_DISTANCE_CHANGE_FOR_UPDATES = 50;
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        String bestProvider = locationManager.getBestProvider(criteria, true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {}
        }
        location = locationManager.getLastKnownLocation(bestProvider);
        localStorage.putFloat("latitude", (float) location.getLatitude());
        localStorage.putFloat("longitude", (float) location.getLongitude());
        localStorage.commit();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener) this);

        return location;
    }
    @Override
    public void onLocationChanged(Location location)
    {
        localStorage.putFloat("latitude", (float) location.getLatitude());
        localStorage.putFloat("longitude", (float) location.getLongitude());
        localStorage.commit();
    }

但是"位置"是空的,为什么?

您可以尝试以下代码片段:

public class LocationProvider
{
    private static final String     TAG         = "DEBUG";
    private static final String[]   provider    = new String[]
                                                {
            LocationManager.GPS_PROVIDER,
            LocationManager.NETWORK_PROVIDER,
            LocationManager.PASSIVE_PROVIDER
                                                };
    public Location getLocationByProvider(Context context)
    {
        Location location = null;
        // LocationManager locationManager = (LocationManager) context.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        try
        {
            for (int i = 0; i < provider.length; i++)
            {
                if (locationManager.isProviderEnabled(provider[i]))
                {
                    // locationManager.requestLocationUpdates(provider[i], 0, 0, locationListenerGps);
                    location = locationManager.getLastKnownLocation(provider[i]);
                    if (location != null)
                    {
                        break;
                    }
                }
            }
        }
        catch (IllegalArgumentException e)
        {
            Log.d(TAG, "Cannot acces Provider " + provider);
        }
        return location;
    }
}

您还可以获取最佳提供商,然后从最佳提供商处获取位置。

mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setCostAllowed(true);
            criteria.setPowerRequirement(Criteria.POWER_LOW);
            bestProvider = mLocationManager.getBestProvider(criteria, true);

希望这对你有帮助。:-)

文档本身表示,在位置不可用的情况下,getLastLocation可能返回null。当它为null时,在OnConnected()方法中调用requestLocationUpdate方法,如下所示:

@Override
public void onConnected(Bundle connectionHint) {
    mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (mCurrentLocation == null) {
        LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
    }
}

在调用requestLocationUpdates之前,请创建mLocationRequest对象:

mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);   
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

当位置可用时,OnLocationChanged将自动被解雇。获取此回调的速率由创建请求对象时在setInterval()中设置的间隔定义。

@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;        
}

届时将更新您的位置对象。

关注此链接:http://developer.android.com/training/location/receive-location-updates.html

由于这篇文章,我解决了类似的问题:在android棉花糖应用程序中定位的权限问题。

事实上,如果你使用compileSdkVersion 23(或更高版本),如果你想让你的应用程序在android 6.0+设备上正确运行,你必须处理新的android权限系统。。。更多信息可以在这里找到:http://developer.android.com/training/permissions/index.html.

测试应用程序的一个快速而肮脏的解决方法是将build.gradle中的compileSdkVersiontargetSdkVersion更改为22…

然而,我认为这是愚蠢的,没有明确的错误信息,只是一个空。。。

相关内容

  • 没有找到相关文章

最新更新