BVtp
BVtp

Reputation: 2480

LocationListener takes too long until first result

I've encountered a problem. If I turn off GPS, and then turn it on again - getLastKnownLocation() returns null. In that case the only way to get current coordinates is the LocationListener. (Correct me if I'm wrong).

So I called the listener :

locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 10, (float) 0.01, locationListener );


 .....

public void onLocationChanged(final Location loc)
        {   
            try {
                addresses = geoCoder.getFromLocation( loc.getLatitude(), loc.getLongitude(), 1);
                if (addresses.size() > 0)
                {
                    String cityName = addresses.get(0).getLocality();
                    String streetName = addresses.get(0).getAddressLine(0);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

It takes about a whole minute(!) to find the location. Although once it finds it , it then updates it every second. But it still doesn't help me much.

I need to find the current coordinates as quickly as possible. How can that be done ? and why does it take the onLocationChanged is called so slowly the first time but much quicker the following times?

Upvotes: 0

Views: 1017

Answers (1)

blackkara
blackkara

Reputation: 5052

If the app needs more accurate and fresh location, use gps provider. Gps provider must be warmed up before getting locations. So being warmed up takes time and changes by where you are. You could use cached gps location calling getLastKnownLocation("gps") until gps hardware warmed up, and check this is too old or not.

Upvotes: 2

Related Questions