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