/[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 319 - (hide annotations) (download)
Fri Sep 11 12:24:53 2009 UTC (14 years, 8 months ago) by torben
File size: 2268 byte(s)
Provider's : differentiate between an empty returned list or a communication error

Re-organize some of the background work
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.util.Log;
11     import dk.thoerup.traininfo.StationBean;
12     import dk.thoerup.traininfo.util.DownloadUtil;
13    
14     public class GoogleStationProvider implements StationProvider {
15    
16     List<StationBean> stations = new ArrayList<StationBean>();
17    
18     @Override
19 torben 319 public boolean lookupStations(Location location) {
20     boolean success = false;
21 torben 253 //ToDo: to be nice put the api key into the request
22     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";
23     //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";
24    
25     try {
26 torben 283 String data = DownloadUtil.getContentString(urlSource, 30000);
27 torben 253 StringBuilder builder = new StringBuilder(data);
28    
29     while (builder.charAt(0) != '{')
30     builder.deleteCharAt(0);
31     while (builder.charAt(builder.length()-1) != '}')
32     builder.deleteCharAt(builder.length()-1);
33    
34     JSONObject json = new JSONObject(builder.toString());
35     // now have some fun with the results...
36     JSONArray res = json.getJSONArray("results");
37    
38     Location tmpLocation = new Location("gps");
39     stations.clear();
40    
41     for (int i=0; i<res.length(); i++) {
42     JSONObject jsonStation = res.getJSONObject(i);
43    
44     double lat = jsonStation.getDouble("lat");
45     double lng = jsonStation.getDouble("lng");
46     String title = jsonStation.getString("title");
47     String addr = jsonStation.getString("streetAddress");
48     tmpLocation.setLatitude(lat);
49     tmpLocation.setLongitude(lng);
50    
51     float distance = location.distanceTo(tmpLocation);
52    
53     stations.add( new StationBean(title,lat,lng,(int)distance,addr) );
54    
55     Log.e("Location", title + " " + lat +"," + lng);
56     }
57    
58 torben 319 success = true;
59 torben 253 } catch (Exception e) {
60     Log.e("Location","Location",e);
61     }
62 torben 319 return success;
63 torben 253 }
64    
65     @Override
66     public List<StationBean> getStations() {
67     return stations;
68     }
69    
70    
71     }

  ViewVC Help
Powered by ViewVC 1.1.20