/[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 433 - (hide annotations) (download)
Sat Oct 10 11:30:08 2009 UTC (14 years, 7 months ago) by torben
File size: 2449 byte(s)
Added station favorite list
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 torben 433 public boolean lookupStationsByName(String name) {
19 torben 381 return false; // Not supported
20     }
21    
22 torben 253 @Override
23 torben 433 public boolean lookupStationsByIds(String ids) {
24     return false;
25     }
26    
27     @Override
28 torben 319 public boolean lookupStations(Location location) {
29     boolean success = false;
30 torben 253 //ToDo: to be nice put the api key into the request
31     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";
32     //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";
33    
34     try {
35 torben 283 String data = DownloadUtil.getContentString(urlSource, 30000);
36 torben 253 StringBuilder builder = new StringBuilder(data);
37    
38     while (builder.charAt(0) != '{')
39     builder.deleteCharAt(0);
40     while (builder.charAt(builder.length()-1) != '}')
41     builder.deleteCharAt(builder.length()-1);
42    
43     JSONObject json = new JSONObject(builder.toString());
44     // now have some fun with the results...
45     JSONArray res = json.getJSONArray("results");
46    
47     Location tmpLocation = new Location("gps");
48     stations.clear();
49    
50     for (int i=0; i<res.length(); i++) {
51     JSONObject jsonStation = res.getJSONObject(i);
52    
53     double lat = jsonStation.getDouble("lat");
54     double lng = jsonStation.getDouble("lng");
55     String title = jsonStation.getString("title");
56     String addr = jsonStation.getString("streetAddress");
57     tmpLocation.setLatitude(lat);
58     tmpLocation.setLongitude(lng);
59    
60     float distance = location.distanceTo(tmpLocation);
61    
62     stations.add( new StationBean(title,lat,lng,(int)distance,addr) );
63    
64     Log.e("Location", title + " " + lat +"," + lng);
65     }
66    
67 torben 319 success = true;
68 torben 253 } catch (Exception e) {
69     Log.e("Location","Location",e);
70     }
71 torben 319 return success;
72 torben 253 }
73    
74     @Override
75     public List<StationBean> getStations() {
76     return stations;
77     }
78    
79    
80     }

  ViewVC Help
Powered by ViewVC 1.1.20