--- android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java 2009/08/28 07:21:04 286 +++ android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java 2009/09/24 08:19:23 341 @@ -10,48 +10,42 @@ import android.os.Bundle; import android.os.Handler; import android.util.Log; -import dk.thoerup.traininfo.provider.ProviderFactory; -import dk.thoerup.traininfo.provider.StationProvider; -public class StationLocator implements LocationListener{ + +public class LocationLookup implements LocationListener{ LocationManager locManager; Context cntx; Handler hndl; - StationProvider provider; Location savedLocation = null; + boolean isSearching = false; boolean hasGps; - public StationLocator(Context c, Handler h) { + public LocationLookup(Context c, Handler h) { cntx = c; hndl = h; - - provider = ProviderFactory.getStationProvider(); } - public List getStations() { - return provider.getStations(); - } - - public void abortLocationListener() { - locManager.removeUpdates(this); - } public boolean hasLocation() { return savedLocation != null; } + + public Location getLocation() + { + return savedLocation; + } public void locateStations() { + + isSearching = true; + hasGps = false; - locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); - Criteria c = new Criteria(); - c.setAccuracy(Criteria.ACCURACY_FINE); - List providers = locManager.getProviders(true); - + List providers = locManager.getProviders(true); if (providers.size() > 0) { for(String provider : providers) { @@ -62,12 +56,16 @@ } } else { // message that no suitable provider was found - hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER); + hndl.sendEmptyMessage(StationList.NOPROVIDER); } } @Override public void onLocationChanged(Location location) { + if (isSearching == false) + return; + Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider()); + if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy()) savedLocation = new Location(location); //save a copy @@ -75,41 +73,31 @@ if (hasGps) { if (!location.getProvider().equals("gps")) { return; // at least give the gps a chance - } else if (location.getAccuracy() > 100) { + } else if (location.getAccuracy() > 256) { return; //if we have a gps provider lets wait for a more precise fix } } - - locManager.removeUpdates(this); - hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); - } - - public void findNearestStations() { - findNearestStations(savedLocation); + stopSearch(); + hndl.sendEmptyMessage(StationList.GOTLOCATION); } - public void findNearestStations(Location location) { - provider.lookupStations(location); - - if ( provider.getStations().size() > 0) - hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST); - else - hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); + public void stopSearch() + { + if (isSearching) { + isSearching = false; + locManager.removeUpdates(this); + } } @Override public void onProviderDisabled(String provider) { - // TODO Auto-generated method stub - } @Override public void onProviderEnabled(String provider) { - // TODO Auto-generated method stub - } @@ -137,11 +125,17 @@ hillerod.setLatitude(55.929177); hillerod.setLongitude(12.308095); + Location aarhus = new Location("gps"); //Aros + aarhus.setLatitude(56.153828); + aarhus.setLongitude(10.200369); + + + 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", hillerod); + lm.setTestProviderLocation("gps2", kbh); } public static void removeMockLocation(Context cntx) {