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

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

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

android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java revision 253 by torben, Mon Aug 10 16:58:22 2009 UTC android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java revision 319 by torben, Fri Sep 11 12:24:53 2009 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
 import java.util.ArrayList;  
3  import java.util.List;  import java.util.List;
4    
 import org.json.JSONArray;  
 import org.json.JSONObject;  
   
5  import android.content.Context;  import android.content.Context;
6  import android.location.Criteria;  import android.location.Criteria;
7  import android.location.Location;  import android.location.Location;
8  import android.location.LocationListener;  import android.location.LocationListener;
9  import android.location.LocationManager;  import android.location.LocationManager;
 import android.location.LocationProvider;  
10  import android.os.Bundle;  import android.os.Bundle;
11  import android.os.Handler;  import android.os.Handler;
12  import android.util.Log;  import android.util.Log;
 import dk.thoerup.traininfo.provider.ProviderFactory;  
 import dk.thoerup.traininfo.provider.StationProvider;  
 import dk.thoerup.traininfo.util.DownloadUtil;  
13    
14  public class StationLocator implements LocationListener{  
15    public class LocationLookup implements LocationListener{
16    
17          LocationManager locManager;          LocationManager locManager;
18          Context cntx;          Context cntx;
19          Handler hndl;          Handler hndl;
20    
         List<StationBean> stationList = new ArrayList<StationBean>();  
   
           
21          Location savedLocation = null;          Location savedLocation = null;
22            
23            boolean hasGps;
24    
25          public StationLocator(Context c, Handler h) {          public LocationLookup(Context c, Handler h) {
26                  cntx = c;                  cntx = c;
27                  hndl = h;                  hndl = h;
28          }                  
           
         public List<StationBean> getStations() {  
                 return stationList;  
29          }          }
30                    
31          public void abortLocationListener() {          public void abortLocationListener() {
32                  locManager.removeUpdates(this);                  locManager.removeUpdates(this);
33          }          }
34            
35            public boolean hasLocation() {
36                    return savedLocation != null;
37            }
38            
39            public Location getLocation()
40            {
41                    return savedLocation;
42            }
43    
44          public void locateStations() {          public void locateStations() {
45                  //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;
   
46                  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);  
                   
47    
48                  if (bestProv != null) {                  List<String> providers = locManager.getProviders(true);        
49                          savedLocation = locManager.getLastKnownLocation(bestProv);                  
50                          locManager.requestLocationUpdates(bestProv, 0, 0, this);                  if (providers.size() > 0) {
51                            for(String provider : providers) {
52                                    Log.i("Provider", ""+provider);
53                                    if (provider.equalsIgnoreCase("gps"))
54                                            hasGps = true;
55                                    locManager.requestLocationUpdates(provider, 0, 0, this);
56                            }
57                  } else {                  } else {
58                          // message that no suitable provider was found                          // message that no suitable provider was found
59                          hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);                          hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);
# Line 75  public class StationLocator implements L Line 63  public class StationLocator implements L
63          public void onLocationChanged(Location location) {          public void onLocationChanged(Location location) {
64                  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());
65    
66                  savedLocation = new Location(location); //save a copy                  if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
67                            savedLocation = new Location(location); //save a copy
68                                    
69                  if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix                  if (hasGps) {
70                          return;                          if (!location.getProvider().equals("gps")) {
71                                    return; // at least give the gps a chance
72                            } else if (location.getAccuracy() > 100) {
73                                    return; //if we have a gps provider lets wait for a more precise fix
74                            }
75    
76                    }
77    
78                  locManager.removeUpdates(this);                  locManager.removeUpdates(this);
79                  hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);                  hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);
80          }          }
81                    /*
82          public void findNearestStations() {  
                 findNearestStations(savedLocation);  
         }  
           
83          public void findNearestStations(Location location) {          public void findNearestStations(Location location) {
84                  StationProvider prov = ProviderFactory.getStationProvider();                  provider.lookupStations(location);
                 prov.lookupStations(location);  
                 this.stationList = prov.getStations();  
85                                    
86                  if (stationList.size() > 0)                  if ( provider.getStations().size() > 0)
87                          hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);                          hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
88                  else                  else
89                          hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);                          hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
90          }          }*/
91    
92    
93          @Override          @Override
94          public void onProviderDisabled(String provider) {          public void onProviderDisabled(String provider) {
                 // TODO Auto-generated method stub  
   
95          }          }
96    
97    
98          @Override          @Override
99          public void onProviderEnabled(String provider) {          public void onProviderEnabled(String provider) {
                 // TODO Auto-generated method stub  
   
100          }          }
101    
102    
# Line 134  public class StationLocator implements L Line 120  public class StationLocator implements L
120                  bjbro.setLatitude(56.380745);                  bjbro.setLatitude(56.380745);
121                  bjbro.setLongitude(9.655609);                  bjbro.setLongitude(9.655609);
122                                    
123                    Location hillerod = new Location("gps");
124                    hillerod.setLatitude(55.929177);
125                    hillerod.setLongitude(12.308095);
126                    
127                    Location aarhus = new Location("gps"); //Aros
128                    aarhus.setLatitude(56.153828);
129                    aarhus.setLongitude(10.200369);
130                    
131                    
132                                    
133                  LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);                  LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
134                  if (lm.getProvider("gps2") == null)                  if (lm.getProvider("gps2") == null)

Legend:
Removed from v.253  
changed lines
  Added in v.319

  ViewVC Help
Powered by ViewVC 1.1.20