/[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

revision 242 by torben, Sun Aug 9 11:48:36 2009 UTC revision 285 by torben, Fri Aug 28 06:34:42 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;
13  import dk.thoerup.traininfo.util.DownloadUtil;  import dk.thoerup.traininfo.provider.ProviderFactory;
14    import dk.thoerup.traininfo.provider.StationProvider;
15    
16  public class StationLocator implements LocationListener{  public class StationLocator implements LocationListener{
17    
# Line 23  public class StationLocator implements L Line 19  public class StationLocator implements L
19          Context cntx;          Context cntx;
20          Handler hndl;          Handler hndl;
21    
22          ArrayList<StationBean> stationList = new ArrayList<StationBean>();          StationProvider provider;
         List<StationBean> safeStationList = java.util.Collections.unmodifiableList(stationList);  
   
           
23          Location savedLocation = null;          Location savedLocation = null;
24    
25          public StationLocator(Context c, Handler h) {          public StationLocator(Context c, Handler h) {
26                  cntx = c;                  cntx = c;
27                  hndl = h;                  hndl = h;
28                    
29                    provider = ProviderFactory.getStationProvider();
30          }          }
31                    
32          public List<StationBean> getStations() {          public List<StationBean> getStations() {
33                  return safeStationList;                  return provider.getStations();
34          }          }
35                    
36          public void abortLocationListener() {          public void abortLocationListener() {
37                  locManager.removeUpdates(this);                  locManager.removeUpdates(this);
38          }          }
39            
40            public boolean hasLocation() {
41                    return savedLocation != null;
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                  //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
# Line 63  public class StationLocator implements L Line 62  public class StationLocator implements L
62                                    
63    
64                  if (bestProv != null) {                  if (bestProv != null) {
                         savedLocation = locManager.getLastKnownLocation(bestProv);  
65                          locManager.requestLocationUpdates(bestProv, 0, 0, this);                          locManager.requestLocationUpdates(bestProv, 0, 0, this);
66                  } else {                  } else {
67                          // message that no suitable provider was found                          // message that no suitable provider was found
# Line 88  public class StationLocator implements L Line 86  public class StationLocator implements L
86          }          }
87                    
88          public void findNearestStations(Location location) {          public void findNearestStations(Location location) {
89                  //ToDo: to be nice put the api key into the request                  provider.lookupStations(location);
90                  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";                  
91                  //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";                  if ( provider.getStations().size() > 0)
   
                 try {  
                         String data = DownloadUtil.getContent(urlSource, 30000, "UTF-8");  
                         StringBuilder builder = new StringBuilder(data);  
   
                         while (builder.charAt(0) != '{')  
                                 builder.deleteCharAt(0);  
                         while (builder.charAt(builder.length()-1) != '}')  
                                 builder.deleteCharAt(builder.length()-1);  
   
                         JSONObject json = new JSONObject(builder.toString());  
                         // now have some fun with the results...  
                         JSONArray res = json.getJSONArray("results");  
   
                         Location tmpLocation = new Location("gps");  
                         stationList.clear();  
                           
                         for (int i=0; i<res.length(); i++) {  
                                 JSONObject jsonStation = res.getJSONObject(i);  
   
                                 double lat = jsonStation.getDouble("lat");  
                                 double lng = jsonStation.getDouble("lng");  
                                 String title = jsonStation.getString("title");  
                                 String addr = jsonStation.getString("streetAddress");  
                                 tmpLocation.setLatitude(lat);  
                                 tmpLocation.setLongitude(lng);  
   
                                 float distance = location.distanceTo(tmpLocation);  
   
                                 stationList.add( new StationBean(title,lat,lng,(int)distance,addr) );  
   
                                 Log.e("Location", title + " " + lat +"," + lng);  
                         }  
92                          hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);                          hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
93                    else
                 } catch (Exception e) {  
                         Log.e("Location","Location",e);  
94                          hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);                          hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
                 }  
                           
95          }          }
96    
97    
# Line 154  public class StationLocator implements L Line 115  public class StationLocator implements L
115    
116          }          }
117                    
118    
119          public static void injectMockLocation(Context cntx) {          public static void injectMockLocation(Context cntx) {
120                  Location loc = new Location("gps2");                  Location odder = new Location("gps2");
121                  loc.setLatitude(56.378084);                  odder.setLatitude(55.976632);
122                  loc.setLongitude(9.659815);                  odder.setLongitude(10.16407);
123                    
124                    Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573
125                    kbh.setLatitude(55.675092);
126                    kbh.setLongitude(12.578573);
127                    
128                    Location bjbro = new Location("gps2");
129                    bjbro.setLatitude(56.380745);
130                    bjbro.setLongitude(9.655609);
131                    
132                    Location hillerod = new Location("gps");
133                    hillerod.setLatitude(55.929177);
134                    hillerod.setLongitude(12.308095);
135                                    
136                  LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);                  LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
137                    if (lm.getProvider("gps2") == null)
138                            lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
139                  lm.setTestProviderEnabled("gps2", true);                  lm.setTestProviderEnabled("gps2", true);
140                  lm.setTestProviderLocation("gps2", loc);                  lm.setTestProviderLocation("gps2", hillerod);
141            }
142            
143            public static void removeMockLocation(Context cntx) {
144                    LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
145                    if (lm.getProvider("gps2") != null)
146                            lm.removeTestProvider("gps2");
147          }          }
148  }  }

Legend:
Removed from v.242  
changed lines
  Added in v.285

  ViewVC Help
Powered by ViewVC 1.1.20