--- android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java 2009/08/10 17:01:02 254 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java 2009/08/28 07:58:50 288 @@ -21,6 +21,8 @@ StationProvider provider; Location savedLocation = null; + + boolean hasGps; public StationLocator(Context c, Handler h) { cntx = c; @@ -36,30 +38,24 @@ public void abortLocationListener() { locManager.removeUpdates(this); } + + public boolean hasLocation() { + return savedLocation != null; + } 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); - - if (bestProv != null) { - savedLocation = locManager.getLastKnownLocation(bestProv); - locManager.requestLocationUpdates(bestProv, 0, 0, this); + List providers = locManager.getProviders(true); + + 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); @@ -69,10 +65,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); @@ -94,15 +97,11 @@ @Override public void onProviderDisabled(String provider) { - // TODO Auto-generated method stub - } @Override public void onProviderEnabled(String provider) { - // TODO Auto-generated method stub - } @@ -126,12 +125,15 @@ bjbro.setLatitude(56.380745); bjbro.setLongitude(9.655609); + Location hillerod = new Location("gps"); + hillerod.setLatitude(55.929177); + hillerod.setLongitude(12.308095); LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); if (lm.getProvider("gps2") == null) lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE ); lm.setTestProviderEnabled("gps2", true); - lm.setTestProviderLocation("gps2", kbh); + lm.setTestProviderLocation("gps2", hillerod); } public static void removeMockLocation(Context cntx) {