Hi Ludwig,

Thanks for the suggestion. I do implement a listener, when the
application opens. But I found that the GPS data using
getLastKnownLocation was often out of date or inaccurate, and that
caused me real problems, since it's a mapping application and the data
needs to be very accurate - I need to warn the user if it isn't. Hence
the polling to make sure it's up to date. (I run the polling in a
background thread.)

I've pasted my listener code below, that I call from onCreate... do
you suggest any changes?

thanks,
Anna

        //This function called from onCreate...
        public boolean testProviders() {
                Log.d(LOG_TAG, "testProviders");
                String location_context = Context.LOCATION_SERVICE;
                locationmanager = (LocationManager) getSystemService
(location_context);
                listener = new LocationListener() {
                        public void onLocationChanged(Location location) {
                        }

                        public void onProviderDisabled(String provider) {
                        }

                        public void onProviderEnabled(String provider) {
                        }

                        public void onStatusChanged(String provider, int status,
                                        Bundle extras) {
                        }
                };

                locationmanager.requestLocationUpdates("gps", 500, 0, listener);

                Log.d(LOG_TAG, "Checking location with provider " + "gps");

                location = locationmanager.getLastKnownLocation("gps");

                if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        latString = latitude.toString();
                        longString = longitude.toString();
                        Log.d(LOG_TAG, "Location found: latitude" + latString
                                        + " and longitude " + longString);
                        return true;

                } else {
                        Log.d(LOG_TAG, "Location not found");
                        return false;
                }


On 2 Feb, 12:50, Ludwig <[email protected]> wrote:
> I suggest you better implement a location listener, which is much more
> light-weight than the busy polling you are implementing. You can cancel your
> subscription to location updates once you have one that satisfies you.Ludwig
>
> 2009/2/1 Anna PS <[email protected]>
>
>
>
> > Great - thank you. I'll remember to read the documentation next
> > time :)
>
> > I've pasted my code below in case anyone wants to borrow it: it keeps
> > polling for up-to-date and accurate GPS data, up to a maximum of 10
> > seconds. If by then the GPS data is old or not accurate enough, it
> > just returns false.
>
> > The code feels a bit dubious, but it seems to do the job.
>
> > Anna
>
> >                        long locationTime = location.getTime();
> >                        long currentTime = System.currentTimeMillis();
> >                        timeDifference = (currentTime - locationTime) /
> > 1000;
>
> >                        float accuracy = location.getAccuracy();
> >                        int count = 0;
>
> >                        // Wait for accurate GPS data, up to a maximum of 10
> > seconds before
> >                        // throwing an error
> >                        while (((timeDifference > 10) || (accuracy > 20.0))
> > && (count <
> > 20)) {
> >                                location =
> > locationmanager.getLastKnownLocation("gps");
> >                                locationTime = location.getTime();
> >                                currentTime = System.currentTimeMillis();
> >                                timeDifference = (currentTime -
> > locationTime) / 1000;
> >                                accuracy = location.getAccuracy();
> >                                Log.d(LOG_TAG, "getting up to date GPS data,
> > time diff = "
> >                                                + timeDifference + " &
> > accuracy = " + accuracy
> >                                                + " & count = " + count);
> >                                try {
> >                                        Thread.currentThread();
> >                                        Thread.sleep(500);
> >                                } catch (InterruptedException ie) {
> >                                }
> >                                count++;
> >                        }
> >                        // No accurate GPS data? Exit here and warn the user
> >                        if ((timeDifference > 10) || (accuracy > 20.0)) {
> >                                return false;
> >                         }
>
> > On 27 Jan, 03:38, gjs <[email protected]> wrote:
> > > Hi,
>
> > > Subtract the Location.getTime() value from the current time to get the
> > > age of the last fix.
>
> > > Seehttp://
> > code.google.com/android/reference/android/location/Location.html
>
> > > Regards
>
> > > On Jan 23, 10:07 am, Anna PS <[email protected]> wrote:
>
> > > > Hi
>
> > > > When you get GPS location using getLastKnownLocation, is there a way
> > > > to check how old the data is, i.e. when the location was last
> > > > updated?
>
> > > > I'm noticing that sometimes my app is giving me an out-of-date
> > > > location (usually because the sky is not visible when the app starts)
> > > > - it'd be good to warn the user about this.
>
> > > > It's possible to check the age of the data on the iPhone I believe, is
> > > > it possible in Android?
>
> > > > thanks!
> > > > Anna
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to