tags:

views:

396

answers:

1

Hello -

I have a few questions (so far) with the the LocationManager on Android and GPS in general.. Feel free to answer any number of the questions below, and I appreciate your help in advance! (I noticed this stuff doesn't appear to be documented very well, so hopefully these questions will help others out too!)

1) I am using the following code, but I think there may be extra fluff in here that I do not need. Can you tell me if I can delete any of this?

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);       
LocationListener locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
LocationProvider locationProvider = lm.getProvider("gps");
Location currentLocation = lm.getLastKnownLocation(locationProvider.getName());

2) Is there a way to hold off on the last step (accessing "getLastKnownLocation" until after I am sure I have a GPS lock? What happens if this is called and GPS is still looking for signal?

3) MOST importantly, I want to ensure I have a GPS lock before I proceed to my next method, so is there a way to check to see if GPS is locked on and getLastKnownLocation is up to date?

4) Is there a way to 'shut down' the GPS listener once it does receive a lock and getLastKnownLocation is updated? I don't see a need to keep this running for my application once I have obtained a lock..

5) Can you please confirm my assumption that "getLastKnownLocation" is updated frequently as the receiver moves?

6) In my code, I also have a class called "MyLocationListener" (code below) that I honestly just took from another example.. Is this actually needed? I assume this updates my location manager whenever the location changes, but it sure doesn't appear that there is much to the class itself!

private class MyLocationListener implements LocationListener 
    {
        @Override
        public void onLocationChanged(Location loc) {
            if (loc != null) {
                //Toast.makeText(getBaseContext(),   "Location changed : Lat: " + loc.getLatitude() + " Lng: " + loc.getLongitude(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }

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

    }     
+3  A: 
  1. Lines 4-5 seem to be doing you no good. Also, LocationManager.GPS_PROVIDER is "gps", so please just use LocationManager.GPS_PROVIDER.

For the following answers, I am assuming that by "GPS lock" you mean "GPS fix".

  1. "Is there a way to hold off on the last step (accessing "getLastKnownLocation" until after I am sure I have a GPS lock?" -- sure, by not calling the method. "What happens if this is called and GPS is still looking for signal?" -- it returns null.

  2. "MOST importantly, I want to ensure I have a GPS lock before I proceed to my next method" -- that is not possible. "so is there a way to check to see if GPS is locked on and getLastKnownLocation is up to date?" -- if getLastKnownLocation() returns null, you do not have a fix.

  3. Unregister your LocationListener via removeUpdates().

  4. Yes, your assumption is correct, at least for some definition of "frequently".

  5. Yes, you need a LocationListener instance to use requestLocationUpdates(). More importantly, when you get called with onLocationChanged() for the first time, that means you got your first fix. Presumably, for how you appear to want to use the code, this is where you would process the fix, then remove the LocationListener per #4 above.

CommonsWare
This is excellent - Thank you!!!
Tyler
CommonWare - Thank you again for your post.. This has helped me considerably. One item that I still question is where you mentioned that it is not possible to ensure that GPS has a fix. Also, in my testing I have noticed that getLastKnownLocation() is not necessarily null if we do not have a fix yet - for example if I obtained a fix, and then stopped GPS service, I believe getLastKnownLocation() still retains the previous location. How would I go about getting my CURRENT location (or seeing if there is one to tell if I have a fix yet)? Any ideas?
Tyler
"How would I go about getting my CURRENT location (or seeing if there is one to tell if I have a fix yet)?" Call `requestLocationUpdates()` and use the first fix you get. Or, call `getLastKnownLocation()`, and if it is not null, call getTime() and figure out how old it is.
CommonsWare