Using LocationListener within a fragment

Rarw picture Rarw · Feb 26, 2013 · Viewed 8.8k times · Source

Can I get a users location by using LocationListener and LocationManager from within a fragment? Or do I need to use the LocationListener from within the underlying activity and use an interface (or some other method) to transfer the data back to the fragment? I am in the process of converting an activity to a fragment due to a change app UI. Getting location was no problem when I was using standalone activities, however, I cannot get any location returned now that I have made the activity into a fragment.

Below is how I am adding the LocationListener. It is declared earlier on as LocationListener locationListener.

private void addLocationListener(){
    locationListener = new LocationListener(){

        @Override
        public void onLocationChanged(Location location) {
            GeoPoint userLoc = processNewLocation(location);
            if(userLoc != null){
                Log.d("USERLOC", userLoc.toString());
                                    //do something with the location    
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
            if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            } else {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
            }
        }

        @Override
        public void onProviderEnabled(String provider) {
            locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
            if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            } else {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

    };
}

NOTE: processNewLocation just converts the location to a GeoPoint. It's not even getting that far because no location is ever obtained.

Here's the code registering the listener with the location manager

public void addLocationManager(){
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
}

private void getLocationFromNetwork(){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, locationListener);
Log.d("GET NETWORK", "Listener registered");
}

private void getLocationFromGPS(){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, locationListener);
Log.d("GET GPS", "Listener registered");
}

private Location getLastLocation(){
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
return lastKnownLocation;
}

Answer

Kaediil picture Kaediil · Feb 26, 2013

In the code you posted above, you don't ever register the LocationListener with the LocationManager. You need code like this from your onProviderEnabled code somewhere in the Fragment creation code:

locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);

if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}

The onProviderEnabled callbacks won't occur until after you register with the location manager.