--- android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java 2009/09/11 12:24:53 319 +++ android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java 2009/09/23 12:51:49 336 @@ -20,17 +20,14 @@ Location savedLocation = null; + boolean isSearching = false; boolean hasGps; public LocationLookup(Context c, Handler h) { cntx = c; hndl = h; - } - public void abortLocationListener() { - locManager.removeUpdates(this); - } public boolean hasLocation() { return savedLocation != null; @@ -42,6 +39,9 @@ } public void locateStations() { + + isSearching = true; + hasGps = false; locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); @@ -56,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 @@ -69,25 +73,22 @@ if (hasGps) { if (!location.getProvider().equals("gps")) { return; // at least give the gps a chance - } else if (location.getAccuracy() > 100) { + } else if (location.getAccuracy() > 128) { return; //if we have a gps provider lets wait for a more precise fix } } - - locManager.removeUpdates(this); - hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); + stopSearch(); + hndl.sendEmptyMessage(StationList.GOTLOCATION); + } + + public void stopSearch() + { + if (isSearching) { + isSearching = false; + locManager.removeUpdates(this); + } } - /* - - public void findNearestStations(Location location) { - provider.lookupStations(location); - - if ( provider.getStations().size() > 0) - hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST); - else - hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); - }*/ @Override