/[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 1006 - (hide annotations) (download)
Mon Aug 2 23:18:53 2010 UTC (13 years, 9 months ago) by torben
File size: 2381 byte(s)
Go away from 2-step station lookup (so now station-cache actually works)

No i just need to do the same operation for the 2 other xml providers
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 torben 577 @Deprecated
14 torben 253 public class GoogleStationProvider implements StationProvider {
15    
16    
17 torben 1006
18     public List<StationBean> lookupStationsByName(String name) {
19     return null; // Not supported
20 torben 381 }
21    
22 torben 253 @Override
23 torben 1006 public List<StationBean> lookupStationsByIds(String ids) {
24     return null;
25 torben 433 }
26    
27     @Override
28 torben 1006 public List<StationBean> lookupStations(Location location) {
29     List<StationBean> stations = new ArrayList<StationBean>();
30    
31 torben 253 //ToDo: to be nice put the api key into the request
32     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";
33     //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";
34    
35     try {
36 torben 283 String data = DownloadUtil.getContentString(urlSource, 30000);
37 torben 253 StringBuilder builder = new StringBuilder(data);
38    
39     while (builder.charAt(0) != '{')
40     builder.deleteCharAt(0);
41     while (builder.charAt(builder.length()-1) != '}')
42     builder.deleteCharAt(builder.length()-1);
43    
44     JSONObject json = new JSONObject(builder.toString());
45     // now have some fun with the results...
46     JSONArray res = json.getJSONArray("results");
47    
48     Location tmpLocation = new Location("gps");
49     stations.clear();
50    
51     for (int i=0; i<res.length(); i++) {
52     JSONObject jsonStation = res.getJSONObject(i);
53    
54     double lat = jsonStation.getDouble("lat");
55     double lng = jsonStation.getDouble("lng");
56     String title = jsonStation.getString("title");
57     String addr = jsonStation.getString("streetAddress");
58     tmpLocation.setLatitude(lat);
59     tmpLocation.setLongitude(lng);
60    
61     float distance = location.distanceTo(tmpLocation);
62    
63     stations.add( new StationBean(title,lat,lng,(int)distance,addr) );
64    
65     Log.e("Location", title + " " + lat +"," + lng);
66     }
67    
68     } catch (Exception e) {
69     Log.e("Location","Location",e);
70 torben 1006 return null;
71 torben 253 }
72     return stations;
73     }
74     }

  ViewVC Help
Powered by ViewVC 1.1.20