/[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 252 by torben, Mon Aug 10 10:49:43 2009 UTC revision 253 by torben, Mon Aug 10 16:58:22 2009 UTC
# Line 15  import android.location.LocationProvider Line 15  import android.location.LocationProvider
15  import android.os.Bundle;  import android.os.Bundle;
16  import android.os.Handler;  import android.os.Handler;
17  import android.util.Log;  import android.util.Log;
18    import dk.thoerup.traininfo.provider.ProviderFactory;
19    import dk.thoerup.traininfo.provider.StationProvider;
20  import dk.thoerup.traininfo.util.DownloadUtil;  import dk.thoerup.traininfo.util.DownloadUtil;
21    
22  public class StationLocator implements LocationListener{  public class StationLocator implements LocationListener{
# Line 23  public class StationLocator implements L Line 25  public class StationLocator implements L
25          Context cntx;          Context cntx;
26          Handler hndl;          Handler hndl;
27    
28          ArrayList<StationBean> stationList = new ArrayList<StationBean>();          List<StationBean> stationList = new ArrayList<StationBean>();
         List<StationBean> safeStationList = java.util.Collections.unmodifiableList(stationList);  
29    
30                    
31          Location savedLocation = null;          Location savedLocation = null;
# Line 35  public class StationLocator implements L Line 36  public class StationLocator implements L
36          }          }
37                    
38          public List<StationBean> getStations() {          public List<StationBean> getStations() {
39                  return safeStationList;                  return stationList;
40          }          }
41                    
42          public void abortLocationListener() {          public void abortLocationListener() {
# Line 88  public class StationLocator implements L Line 89  public class StationLocator implements L
89          }          }
90                    
91          public void findNearestStations(Location location) {          public void findNearestStations(Location location) {
92                  //ToDo: to be nice put the api key into the request                  StationProvider prov = ProviderFactory.getStationProvider();
93                  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";                  prov.lookupStations(location);
94                  //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";                  this.stationList = prov.getStations();
95                    
96                  try {                  if (stationList.size() > 0)
                         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);  
                         }  
97                          hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);                          hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
98                    else
                 } catch (Exception e) {  
                         Log.e("Location","Location",e);  
99                          hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);                          hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
                 }  
                           
100          }          }
101    
102    
# Line 153  public class StationLocator implements L Line 119  public class StationLocator implements L
119                  // TODO Auto-generated method stub                  // TODO Auto-generated method stub
120    
121          }          }
122            
123    
124          public static void injectMockLocation(Context cntx) {          public static void injectMockLocation(Context cntx) {
125                  Location odder = new Location("gps2");                  Location odder = new Location("gps2");
# Line 163  public class StationLocator implements L Line 130  public class StationLocator implements L
130                  kbh.setLatitude(55.675092);                  kbh.setLatitude(55.675092);
131                  kbh.setLongitude(12.578573);                  kbh.setLongitude(12.578573);
132                                    
133                    Location bjbro = new Location("gps2");
134                    bjbro.setLatitude(56.380745);
135                    bjbro.setLongitude(9.655609);
136                    
137                    
138                  LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);                  LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
139                  if (lm.getProvider("gps2") == null)                  if (lm.getProvider("gps2") == null)
140                          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 );

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

  ViewVC Help
Powered by ViewVC 1.1.20