requestLocationUpdate takes too long
Asked Answered
D

4

6

I have only one device (HTC HD Deside) for testing my apps and when I want to get GPS data with

locationManager.requestLocationUpdate(criteria,0,0,this); //critera = "gps"

My GPS icon appears in the notification bar but it "flashes", as if It meant my device is looking for and get the GPS data. Accordingly, my apps takes too much "thinking" times before it succeeds to get data from GPS (~1mn to 3mn !) (before the icon stop flashes)

So, I'm obliged to create a while instruction like this :

do{
    lastLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}while(System.currentTimeMillis() - lastLocation.getTime() > 5000);

Thanks that, I'm sure to get a recently location but I lost too much time.

What's the problem? is it the hardware? Can I find a better way for fix this? Can I get "quickly" the GPS data? :/ (1~5 sec)

Donetta answered 10/3, 2013 at 21:45 Comment(3)
Do you require the finer granularity of GPS or could you get by with another provider?Uncap
I need use GPS because my app will be use in location without wifi/internet ^^ So, I must use GPS (the only way for get data latitude/longitude/altitude anywhere?)Donetta
You don't need to wait (or poll) for the last known location. The location listener will be called as soon as a fix is available. The last known location may be useful if a fix is available from an earlier GPS request. If none is available null is returned (which would crash your loop, by the way).Macpherson
R
2

GPS sensor may take more time when you are inside a building or covered area. Sometimes if you are inside a building and no GPS signals available, its always blinking and draining the battery a lot. Try to utilize some other provider.

Refectory answered 11/3, 2013 at 3:43 Comment(0)
B
4

I think you should read this http://developer.android.com/guide/topics/location/strategies.html guide prior developing an location aware app.

The GPS sensor needs 1 to 3 minutes to get a fix, you can't have a very quick fix(1-5 seconds as you want).

You should use the last known location value and start requesting location fixes. When you have one, you refresh your content accordingly.

Brauer answered 10/3, 2013 at 23:35 Comment(0)
R
2

GPS sensor may take more time when you are inside a building or covered area. Sometimes if you are inside a building and no GPS signals available, its always blinking and draining the battery a lot. Try to utilize some other provider.

Refectory answered 11/3, 2013 at 3:43 Comment(0)
G
0

Are you connected to a data network?

I don't know about HTC, but generally when a data connection is available, the device uses something called Assisted GPS. Instead of scanning all the visible satellite over the horizon the satellite ephemeris is downloaded from servers and the time taken to get a fix is 10-20 seconds.

Also it is possible that you are indoors or a low signal area or if you have a bad antenna.

Use the GPS test application from the market to check if antenna has a problem (by comparing two devices).

Groome answered 12/3, 2013 at 7:52 Comment(1)
Yes, i did it ! and the problem is same. So...as you said , the problem is a a low signal area I think.Donetta
M
0

GPS provider sometimes is battery energy hungry and also needs to retrieve the GPS signal, warm up the hardware and then get the location, this could take some time if there is the first time using it.

What I recommend is to use the new LocationRequest which will select between GPS or NETWORK to get the location if needed.

Firs in onCreate I setup my locationRequest

private fun setupLocationRequest(){
        locationCallback = LocationCallback()
        locationRequest = LocationRequest.create()
        locationRequest.interval = 10000
        locationRequest.fastestInterval = 2000
        locationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
    }

as you can see here PRIORITY_BALANCED_POWER_ACCURACY will give me a block of distance max for latitude and longitude and also will not just use GPS but it will use NETWORK if needed.

Then, I request first the lastKnowLocation, if this location is not retrieved , you can request location updates

fusedLocationClient.lastLocation.addOnSuccessListener { lastLocation ->
                if(lastLocation != null){
                    calculateDistanceAndNavigate(lat,lng,lastLocation.latitude,lastLocation.longitude,radio)
                }else{
                    startLocationUpdates()
                    locationCallback = object : LocationCallback() {
                        override fun onLocationResult(locationResult: LocationResult?) {
                            locationResult ?: return
                            for (location in locationResult.locations) {
                                calculateDistanceAndNavigate(lat,lng,location.latitude,location.longitude,radio)
                            }
                        }
                    }
                }
            }

  private fun startLocationUpdates() {
        fusedLocationClient.requestLocationUpdates(
            locationRequest,
            locationCallback,
            Looper.getMainLooper()
        )
    }

For me this is the most efficient way to get the location, first I dont use just GPS but instead I save battery and also use NETWORK or WIFI if needed, then I request the lastKnownLocation if it is saved (maybe by another app, eg: google maps) and then if that fails I just start requesting location updates (which could take a little bit)

Masquerade answered 11/5, 2020 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.