1

I use LocationClient to get current location and if it fails then use LocationManager.

Some times I don't get current location of device. If it is getting location of device then it keeps getting every time I try and, if it is not able to get location and no matter how many times I try, it never gives the location. Also at the same time if I run code on multiple devices it gives location of some devices but not able to get for rest of the devices.

@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;
}
Geek
  • 8,280
  • 17
  • 73
  • 137
  • related : http://stackoverflow.com/questions/6994904/current-location-is-always-null – petey Mar 19 '14 at 16:21
  • 1
    @petey What is the meaning of giving link of question where answer of the question is code what I already have. Also I have permission. – Geek Mar 19 '14 at 16:55

0 Answers0