tags:

views:

86

answers:

3

I am trying to search for best provider with this case below:

// GPS
    case R.id.main_menu_gps:

        // Set up location services
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        mBestProvider = mLocationManager.getBestProvider(new Criteria(), true);
        Log.d(DEB_TAG, "@@@@Value of mBestProvider is " + mBestProvider);

        if(mBestProvider != null){
            showGpsSearchingDialog();
            mLocationManager.requestLocationUpdates(mBestProvider, 150000, 1, this);
        } else {
            Log.d(DEB_TAG, "Provider is null");
            showGpsAlertDialog();
        }

    break;

My device is returning "GPS" as the best provider but is not able to find a location and my progress dialogue is displayed forever searching. If I go into the phone settings of "Location/Security" and check the "Use wireless networks" the best provider is Network and it works to return a location.

Am I doing something wrong when the best provider is GPS and no data is returned?

A: 

You aren't really doing anything wrong, there are just certain techniques for gaining the location via GPS. The main issue is that the GPS location may be the best available provider (in terms of accuracy) however it may not be able to obtain the current location do to network/structural obstruction. Without logic in place to determine the amount of time that has gone by while the provider has attempted to attain the location unsuccessfully or without the accuracy required; you may never get the location via GPS and will have to use a different provider or the last known location as a fall-back.

Quintin Robinson
You're correct, I went outside and the GPS works. I understand that I should try to write a "timer" into the code and if a certain amount of time has passed, try another provider. Can you point me to any examples that show a concept as such?
taraloca
I'm busy at work ATM but as soon as I can I will.
Quintin Robinson
Thank you, I will check back later.
taraloca
A: 

If GPS is enabled it will always return GPS as the best provider since you specified an empty criteria. It can take a decently delay for a GPS fix and if you are in a building you may never get a fix at all. So if the network location will be acceptable then you should just do network. Or have a timeout so that at some point you stop waiting on gps and then switch to network instead. If you are using a MapView, you may want to use the MyLocationOverlay because it's internal logic will handle that for you.

Your code looks fine btw.

BrennaSoft
Thank you again...I am not using a MapView so I am going to work on figuring out how to code this next portion. :)
taraloca
+1  A: 

You may take a look at my strategy to choose best provider http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-an/3145655#3145655

Fedor
How would I use this in my "case" statement? I am trying to figure out how to call it, allow a dialogue to display while searching, and then to return with the current location results.
taraloca
That's exactly what my code does. You can call myLocation.getLocation() and display some progress indicator. When callback is called you've got the location data.
Fedor
I thank you, but I am stumped. I will update my code in my answer to show you what I am attempting. If anyone can let me know what's going on...great. Sorry, I am trying to learn and understand.
taraloca
No problem. Glad to help!
Fedor