/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java
ViewVC logotype

Diff of /android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 254 by torben, Mon Aug 10 17:01:02 2009 UTC revision 288 by torben, Fri Aug 28 07:58:50 2009 UTC
# Line 21  public class StationLocator implements L Line 21  public class StationLocator implements L
21    
22          StationProvider provider;          StationProvider provider;
23          Location savedLocation = null;          Location savedLocation = null;
24            
25            boolean hasGps;
26    
27          public StationLocator(Context c, Handler h) {          public StationLocator(Context c, Handler h) {
28                  cntx = c;                  cntx = c;
# Line 36  public class StationLocator implements L Line 38  public class StationLocator implements L
38          public void abortLocationListener() {          public void abortLocationListener() {
39                  locManager.removeUpdates(this);                  locManager.removeUpdates(this);
40          }          }
41            
42            public boolean hasLocation() {
43                    return savedLocation != null;
44            }
45    
46          public void locateStations() {          public void locateStations() {
47                  //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;
   
48                  locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);                  locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
                 /*//testcode  
                 List<String> 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);  
                   
49    
50                  if (bestProv != null) {                  List<String> providers = locManager.getProviders(true);        
51                          savedLocation = locManager.getLastKnownLocation(bestProv);                  
52                          locManager.requestLocationUpdates(bestProv, 0, 0, this);                  if (providers.size() > 0) {
53                            for(String provider : providers) {
54                                    Log.i("Provider", ""+provider);
55                                    if (provider.equalsIgnoreCase("gps"))
56                                            hasGps = true;
57                                    locManager.requestLocationUpdates(provider, 0, 0, this);
58                            }
59                  } else {                  } else {
60                          // message that no suitable provider was found                          // message that no suitable provider was found
61                          hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);                          hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);
# Line 69  public class StationLocator implements L Line 65  public class StationLocator implements L
65          public void onLocationChanged(Location location) {          public void onLocationChanged(Location location) {
66                  Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());                  Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
67    
68                  savedLocation = new Location(location); //save a copy                  if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
69                            savedLocation = new Location(location); //save a copy
70                                    
71                  if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix                  if (hasGps) {
72                          return;                          if (!location.getProvider().equals("gps")) {
73                                    return; // at least give the gps a chance
74                            } else if (location.getAccuracy() > 100) {
75                                    return; //if we have a gps provider lets wait for a more precise fix
76                            }
77    
78                    }
79    
80                  locManager.removeUpdates(this);                  locManager.removeUpdates(this);
81                  hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);                  hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);
# Line 94  public class StationLocator implements L Line 97  public class StationLocator implements L
97    
98          @Override          @Override
99          public void onProviderDisabled(String provider) {          public void onProviderDisabled(String provider) {
                 // TODO Auto-generated method stub  
   
100          }          }
101    
102    
103          @Override          @Override
104          public void onProviderEnabled(String provider) {          public void onProviderEnabled(String provider) {
                 // TODO Auto-generated method stub  
   
105          }          }
106    
107    
# Line 126  public class StationLocator implements L Line 125  public class StationLocator implements L
125                  bjbro.setLatitude(56.380745);                  bjbro.setLatitude(56.380745);
126                  bjbro.setLongitude(9.655609);                  bjbro.setLongitude(9.655609);
127                                    
128                    Location hillerod = new Location("gps");
129                    hillerod.setLatitude(55.929177);
130                    hillerod.setLongitude(12.308095);
131                                    
132                  LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);                  LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
133                  if (lm.getProvider("gps2") == null)                  if (lm.getProvider("gps2") == null)
134                          lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );                          lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
135                  lm.setTestProviderEnabled("gps2", true);                  lm.setTestProviderEnabled("gps2", true);
136                  lm.setTestProviderLocation("gps2", kbh);                  lm.setTestProviderLocation("gps2", hillerod);
137          }          }
138                    
139          public static void removeMockLocation(Context cntx) {          public static void removeMockLocation(Context cntx) {

Legend:
Removed from v.254  
changed lines
  Added in v.288

  ViewVC Help
Powered by ViewVC 1.1.20