Android当前位置返回null

时间:2014-03-19 16:12:08

标签: java android geolocation locationmanager location-client

我使用LocationClient获取当前位置,如果失败则使用LocationManager

有时我无法获取设备的当前位置。如果它正在获取设备的位置,那么它每次我都会不断获取,如果它无法获得位置,无论我尝试多少次,它都不会给出位置。同时,如果我在多个设备上运行代码,它会提供某些设备的位置,但无法获得其他设备。

@Override
public void onConnected(Bundle connectionHint) {

    Location loc = locationClient.getLastLocation();
    Log.e(TAG, "location using client = " + loc.getLatitude() + ","
                + loc.getLongitude());

    // If location is not null
    if (loc != null && loc.getLatitude() != 0.0 && loc.getLongitude() != 0.0) {
        // code
    } else {
        // Use location manager to get location
        getCurrentLocationUsingLocationManager();
    }
}



private void getCurrentLocationUsingLocationManager() {
    // Get last known location
    Location location = locationManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);

    // If last known location is recent then use it, otherwise request for
    // location update.
    if (location != null
            && location.getTime() > Calendar.getInstance()
                    .getTimeInMillis()
                    - THRESHOLD_ON_LAST_KNOWN_LOCATION_TIME * 1000)
    {

        // code

    } else
    {

        LocationListener locationListener = getLocationListener();

        // If Network provider is enabled, then start listening for location
        // updates
        if (locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER))
        {

            locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 0, 0,
                    locationListener);

        }

        // If GPS provider is enabled, then start listening for location
        // updates
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
        {

            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 0, 0, locationListener);

        }

        // If none of the location providers is enabled
        if (!locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER)
                && !locationManager
                        .isProviderEnabled(LocationManager.GPS_PROVIDER))
        {

            Log.d(TAG, "GPS and Network providers are disabled.");
        }
    }
}

// Initializes LocationListener to listen for location updates and return an
// instance of it.
private LocationListener getLocationListener()
{
    // Define a listener that responds to location updates
    LocationListener locationListener = new LocationListener()
    {
        @Override
        public void onLocationChanged(Location location)
        {

            // If location is not null and its accuracy requirement is
            // satisfied then,
            if (location != null
                    && location.getAccuracy() <= LOCATION_ACCURACY)
            {

                Log.d(TAG, "Location : " + location.getLatitude() + ", "
                        + location.getLongitude());

                locationManager.removeUpdates(this);
            }
        }

        @Override
        public void onStatusChanged(String provider, int status,
                Bundle extras)
        {

        }

        @Override
        public void onProviderEnabled(String provider)
        {

        }

        @Override
        public void onProviderDisabled(String provider)
        {

        }
    };

    return locationListener;
}

0 个答案:

没有答案