--- android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java 2009/08/28 06:34:42 285 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java 2009/08/28 07:21:04 286 @@ -21,6 +21,8 @@ StationProvider provider; Location savedLocation = null; + + boolean hasGps; public StationLocator(Context c, Handler h) { cntx = c; @@ -42,27 +44,22 @@ } public void locateStations() { - //http://www.google.com/uds/GlocalSearch?callback=google.search.LocalSearch.RawCompletion&context=1&lstkp=0&rsz=small&hl=en&source=gsc&gss=.com&sig=fadf0e8d483d0f70bea11d5905010a16&q=Train%20station&near=56.377424%2C9.656695&key=ABQIAAAA1XbMiDxx_BTCY2_FkPh06RRaGTYH6UMl8mADNa0YKuWNNa8VNxQEerTAUcfkyrr6OwBovxn7TDAH5Q&v=1.0&nocache=1249640467498 + hasGps = false; locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); - /*//testcode - List provs = locManager.getAllProviders(); - for (String p : provs) { - Log.e("Provider", p); - } - provs = locManager.getProviders(true); - for (String p : provs) { - Log.e("ActiveProvider", p); - } - */ + Criteria c = new Criteria(); c.setAccuracy(Criteria.ACCURACY_FINE); - String bestProv = locManager.getBestProvider(c, true); - Log.i("BestProvider", ""+bestProv); + List providers = locManager.getProviders(true); - - if (bestProv != null) { - locManager.requestLocationUpdates(bestProv, 0, 0, this); + + if (providers.size() > 0) { + for(String provider : providers) { + Log.i("Provider", ""+provider); + if (provider.equalsIgnoreCase("gps")) + hasGps = true; + locManager.requestLocationUpdates(provider, 0, 0, this); + } } else { // message that no suitable provider was found hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER); @@ -72,10 +69,17 @@ public void onLocationChanged(Location location) { Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider()); - savedLocation = new Location(location); //save a copy + if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy()) + savedLocation = new Location(location); //save a copy - if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix - return; + if (hasGps) { + if (!location.getProvider().equals("gps")) { + return; // at least give the gps a chance + } else if (location.getAccuracy() > 100) { + return; //if we have a gps provider lets wait for a more precise fix + } + + } locManager.removeUpdates(this); hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);