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

Annotation of /android/TrainInfo/src/dk/thoerup/traininfo/provider/GoogleStationProvider.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 253 - (hide annotations) (download)
Mon Aug 10 16:58:22 2009 UTC (14 years, 9 months ago) by torben
File size: 2275 byte(s)
Refactored the station lookup into a provider interface
1 torben 253 package dk.thoerup.traininfo.provider;
2    
3     import java.util.ArrayList;
4     import java.util.List;
5    
6     import org.json.JSONArray;
7     import org.json.JSONObject;
8    
9     import android.location.Location;
10     import android.os.Handler;
11     import android.util.Log;
12     import dk.thoerup.traininfo.StationBean;
13     import dk.thoerup.traininfo.TrainInfoList;
14     import dk.thoerup.traininfo.util.DownloadUtil;
15    
16     public class GoogleStationProvider implements StationProvider {
17    
18     List<StationBean> stations = new ArrayList<StationBean>();
19    
20     @Override
21     public void lookupStations(Location location) {
22     //ToDo: to be nice put the api key into the request
23     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";
24     //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";
25    
26     try {
27     String data = DownloadUtil.getContent(urlSource, 30000, "UTF-8");
28     StringBuilder builder = new StringBuilder(data);
29    
30     while (builder.charAt(0) != '{')
31     builder.deleteCharAt(0);
32     while (builder.charAt(builder.length()-1) != '}')
33     builder.deleteCharAt(builder.length()-1);
34    
35     JSONObject json = new JSONObject(builder.toString());
36     // now have some fun with the results...
37     JSONArray res = json.getJSONArray("results");
38    
39     Location tmpLocation = new Location("gps");
40     stations.clear();
41    
42     for (int i=0; i<res.length(); i++) {
43     JSONObject jsonStation = res.getJSONObject(i);
44    
45     double lat = jsonStation.getDouble("lat");
46     double lng = jsonStation.getDouble("lng");
47     String title = jsonStation.getString("title");
48     String addr = jsonStation.getString("streetAddress");
49     tmpLocation.setLatitude(lat);
50     tmpLocation.setLongitude(lng);
51    
52     float distance = location.distanceTo(tmpLocation);
53    
54     stations.add( new StationBean(title,lat,lng,(int)distance,addr) );
55    
56     Log.e("Location", title + " " + lat +"," + lng);
57     }
58    
59    
60     } catch (Exception e) {
61     Log.e("Location","Location",e);
62     }
63     }
64    
65     @Override
66     public List<StationBean> getStations() {
67     return stations;
68     }
69    
70    
71     }

  ViewVC Help
Powered by ViewVC 1.1.20