/[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 1066 - (hide annotations) (download)
Thu Sep 16 15:32:42 2010 UTC (13 years, 8 months ago) by torben
File size: 2369 byte(s)
Experimental #5, update TrainInfo client to use common data-beans
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 torben 1066 import dk.thoerup.android.traininfo.common.StationBean;
12 torben 253 import dk.thoerup.traininfo.util.DownloadUtil;
13 torben 577 @Deprecated
14 torben 253 public class GoogleStationProvider implements StationProvider {
15    
16    
17 torben 1006
18 torben 1066 public StationBean lookupStationsByName(String name) {
19 torben 1006 return null; // Not supported
20 torben 381 }
21    
22 torben 253 @Override
23 torben 1066 public StationBean lookupStationsByIds(String ids) {
24 torben 1006 return null;
25 torben 433 }
26    
27     @Override
28 torben 1066 public StationBean lookupStations(Location location) {
29 torben 1006
30 torben 1066 StationBean stations = new StationBean();
31     /*
32 torben 253 //ToDo: to be nice put the api key into the request
33     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";
34     //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";
35    
36     try {
37 torben 283 String data = DownloadUtil.getContentString(urlSource, 30000);
38 torben 253 StringBuilder builder = new StringBuilder(data);
39    
40     while (builder.charAt(0) != '{')
41     builder.deleteCharAt(0);
42     while (builder.charAt(builder.length()-1) != '}')
43     builder.deleteCharAt(builder.length()-1);
44    
45     JSONObject json = new JSONObject(builder.toString());
46     // now have some fun with the results...
47     JSONArray res = json.getJSONArray("results");
48    
49     Location tmpLocation = new Location("gps");
50     stations.clear();
51    
52     for (int i=0; i<res.length(); i++) {
53     JSONObject jsonStation = res.getJSONObject(i);
54    
55     double lat = jsonStation.getDouble("lat");
56     double lng = jsonStation.getDouble("lng");
57     String title = jsonStation.getString("title");
58     String addr = jsonStation.getString("streetAddress");
59     tmpLocation.setLatitude(lat);
60     tmpLocation.setLongitude(lng);
61    
62     float distance = location.distanceTo(tmpLocation);
63    
64     stations.add( new StationBean(title,lat,lng,(int)distance,addr) );
65    
66     Log.e("Location", title + " " + lat +"," + lng);
67     }
68    
69     } catch (Exception e) {
70     Log.e("Location","Location",e);
71 torben 1006 return null;
72 torben 1066 }*/
73 torben 253 return stations;
74     }
75     }

  ViewVC Help
Powered by ViewVC 1.1.20