--- android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java 2009/08/09 09:20:45 240 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java 2009/08/09 11:21:30 241 @@ -1,25 +1,21 @@ package dk.thoerup.traininfo; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONObject; -import dk.thoerup.traininfo.util.DownloadUtil; - import android.content.Context; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; +import android.location.LocationProvider; import android.os.Bundle; import android.os.Handler; import android.util.Log; +import dk.thoerup.traininfo.util.DownloadUtil; public class StationLocator implements LocationListener{ @@ -30,6 +26,9 @@ ArrayList stationList = new ArrayList(); List safeStationList = java.util.Collections.unmodifiableList(stationList); + + Location savedLocation = null; + public StationLocator(Context c, Handler h) { cntx = c; hndl = h; @@ -61,8 +60,10 @@ c.setAccuracy(Criteria.ACCURACY_FINE); String bestProv = locManager.getBestProvider(c, true); Log.e("BestProvider", bestProv); + if (bestProv != null) { + savedLocation = locManager.getLastKnownLocation(bestProv); locManager.requestLocationUpdates(bestProv, 0, 0, this); } else { // message that no suitable provider was found @@ -73,12 +74,20 @@ 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 (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix return; locManager.removeUpdates(this); hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); - + } + + public void findNearestStations() { + findNearestStations(savedLocation); + } + + public void findNearestStations(Location location) { //ToDo: to be nice put the api key into the request String urlSource = "http://www.google.com/uds/GlocalSearch?callback=google.search.LocalSearch.RawCompletion&context=1&q=Train%20station&near=" + location.getLatitude() + "%2C" + location.getLongitude() + "&v=1.0"; //String urlSource = "http://www.google.com/uds/GlocalSearch?callback=google.search.LocalSearch.RawCompletion&context=1&q=Train%20station&near=56.2%2C9.0&v=1.0"; @@ -121,7 +130,7 @@ Log.e("Location","Location",e); hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); } - + } @@ -144,4 +153,15 @@ // TODO Auto-generated method stub } + + public static void injectMockLocation(Context cntx) { + Location loc = new Location("gps2"); + loc.setLatitude(56.378084); + loc.setLongitude(9.659815); + + LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); + lm.setTestProviderEnabled("gps2", true); + lm.setTestProviderLocation("gps2", loc); + + } }