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

  ViewVC Help
Powered by ViewVC 1.1.20