Search for Best Provider in Android Application
I am trying to search for best provider with this case below:
// GPS
case R.id.main_menu_gps:
// Set up loca开发者_JAVA百科tion 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?
You may take a look at my strategy to choose best provider What is the simplest and most robust way to get the user's current location on Android?
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.
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.
精彩评论