13

I have this code to get the best available provider

lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

locationListener = new MyLocationListener();
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(criteria, true);
Location mostRecentLocation = lm.getLastKnownLocation(provider);
if(mostRecentLocation != null) {
    latid=mostRecentLocation.getLatitude();
    longid=mostRecentLocation.getLongitude();
}
lm.requestLocationUpdates(provider, 1, 0, locationListener);

and then the listener

private class MyLocationListener implements LocationListener {

@Override
public void onLocationChanged(Location loc) {
  if (loc != null) {
    latid = loc.getLatitude();
    longid = loc.getLongitude();
    // if(loc.hasAccuracy()==true){
    accuracyd = loc.getAccuracy();
    String providershown = loc.getProvider();    
    accuracy.setText("Location Acquired. Accuracy:"
      + Double.toString(accuracyd) + "m\nProvider: "+providershown);
    accuracy.setBackgroundColor(Color.GREEN);
    // }
    userinfo=usernamevalue+"&"+Double.toString(latid)+"&"+Double.toString(longid);
    submituserlocation(userinfo);
   }
}

When I tested it to a device(htc magic) I found out that when gps is disabled it locks from the network immediately. When I enable the gps it doesnt take any data from the network and waits till it locks from the gps.
I would like to lock the position like the google maps that until they have a good gps signal they use the network to determine my location.
I though the best criteria would do that but what they do is pick a provider once.
Is there something wrong with my code or I have to do threads and timeouts etc to make it happen?

RandomMooCow
  • 734
  • 9
  • 23
spagi
  • 1,457
  • 4
  • 16
  • 19
  • spagi your code helped me greatly - nowhere on the android dev site is it explained how to get the best possible provider. Some devices might have gps, some gsm, some wifi. – Hein du Plessis Feb 16 '11 at 06:38
  • Here is a very interesting Google article: http://developer.android.com/training/basics/location/currentlocation.html – shkschneider Oct 16 '12 at 09:30

2 Answers2

14

Maybe you can try listening to both the network provider and gps provider for a certain amount of time and then check the results from the two. If you don't have results from the gps, use the network results instead. That's how I did it.

capecrawler
  • 67,353
  • 7
  • 31
  • 34
  • So I make 2 location listeners, one for Gps and one for network and initialize them both with requestLocationUpdates. I can listen to them both then with one onLocationChanged? – spagi May 27 '10 at 10:39
  • Yes you should listen to both. You can even use lastKnownLocation before update comes from any provider. – Fedor May 27 '10 at 11:27
  • 1
    You can add a Location variable in your MyLocationListener and set its value within the onLocationChanged(). In your main activity, create two instance of MylocationListener, one is for the gps and one is for the network provider. Listen to both from a certain amount of time (you could implement this by creating a timer thread) and when the time is up, stop listening and check first the location variable in the gpslistener if there is any, else, check the networklistener. – capecrawler May 28 '10 at 01:23
  • If you don't care which provider your location comes from (such as GPS or network) you could use the same LocationListener for both. You'd subscribe them using: manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener) for GPS or manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, myLocationListener) for network locations. You can change the zeros to tweak performance (first zero is min time between updates, the second is min meters moved before an update is received). – AgentKnopf Sep 10 '12 at 07:42
3

Maybe you can use getBestProvider (Criteria criteria, boolean enabledOnly) of LocationManager with criteria as in this other thread. See the official documentation.

Hope it helps

Community
  • 1
  • 1
Filippo Mazza
  • 4,339
  • 4
  • 22
  • 25