/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/provider/GoogleStationProvider.java
ViewVC logotype

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/provider/GoogleStationProvider.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1006 - (show 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 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 @Deprecated
14 public class GoogleStationProvider implements StationProvider {
15
16
17
18 public List<StationBean> lookupStationsByName(String name) {
19 return null; // Not supported
20 }
21
22 @Override
23 public List<StationBean> lookupStationsByIds(String ids) {
24 return null;
25 }
26
27 @Override
28 public List<StationBean> lookupStations(Location location) {
29 List<StationBean> stations = new ArrayList<StationBean>();
30
31 //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 String data = DownloadUtil.getContentString(urlSource, 30000);
37 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 return null;
71 }
72 return stations;
73 }
74 }

  ViewVC Help
Powered by ViewVC 1.1.20