1 |
torben |
237 |
package dk.thoerup.traininfo;
|
2 |
|
|
|
3 |
|
|
import java.io.BufferedReader;
|
4 |
|
|
import java.io.InputStreamReader;
|
5 |
|
|
import java.net.URL;
|
6 |
|
|
import java.net.URLConnection;
|
7 |
|
|
import java.util.ArrayList;
|
8 |
|
|
import java.util.List;
|
9 |
|
|
|
10 |
|
|
import org.json.JSONArray;
|
11 |
|
|
import org.json.JSONObject;
|
12 |
|
|
|
13 |
torben |
238 |
import dk.thoerup.traininfo.util.DownloadUtil;
|
14 |
|
|
|
15 |
torben |
237 |
import android.content.Context;
|
16 |
|
|
import android.location.Criteria;
|
17 |
|
|
import android.location.Location;
|
18 |
|
|
import android.location.LocationListener;
|
19 |
|
|
import android.location.LocationManager;
|
20 |
|
|
import android.os.Bundle;
|
21 |
|
|
import android.os.Handler;
|
22 |
|
|
import android.util.Log;
|
23 |
|
|
|
24 |
|
|
public class StationLocator implements LocationListener{
|
25 |
|
|
|
26 |
|
|
LocationManager locManager;
|
27 |
|
|
Context cntx;
|
28 |
|
|
Handler hndl;
|
29 |
|
|
|
30 |
|
|
ArrayList<StationBean> stationList = new ArrayList<StationBean>();
|
31 |
|
|
List<StationBean> safeStationList = java.util.Collections.unmodifiableList(stationList);
|
32 |
|
|
|
33 |
|
|
public StationLocator(Context c, Handler h) {
|
34 |
|
|
cntx = c;
|
35 |
|
|
hndl = h;
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
public List<StationBean> getStations() {
|
39 |
|
|
return safeStationList;
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
public void abortLocationListener() {
|
43 |
|
|
locManager.removeUpdates(this);
|
44 |
|
|
}
|
45 |
|
|
|
46 |
|
|
public void locateStations() {
|
47 |
|
|
//http://www.google.com/uds/GlocalSearch?callback=google.search.LocalSearch.RawCompletion&context=1&lstkp=0&rsz=small&hl=en&source=gsc&gss=.com&sig=fadf0e8d483d0f70bea11d5905010a16&q=Train%20station&near=56.377424%2C9.656695&key=ABQIAAAA1XbMiDxx_BTCY2_FkPh06RRaGTYH6UMl8mADNa0YKuWNNa8VNxQEerTAUcfkyrr6OwBovxn7TDAH5Q&v=1.0&nocache=1249640467498
|
48 |
|
|
|
49 |
|
|
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
|
50 |
|
|
/*//testcode
|
51 |
|
|
List<String> provs = locManager.getAllProviders();
|
52 |
|
|
for (String p : provs) {
|
53 |
|
|
Log.e("Provider", p);
|
54 |
|
|
}
|
55 |
|
|
provs = locManager.getProviders(true);
|
56 |
|
|
for (String p : provs) {
|
57 |
|
|
Log.e("ActiveProvider", p);
|
58 |
|
|
}
|
59 |
|
|
*/
|
60 |
|
|
Criteria c = new Criteria();
|
61 |
|
|
c.setAccuracy(Criteria.ACCURACY_FINE);
|
62 |
|
|
String bestProv = locManager.getBestProvider(c, true);
|
63 |
|
|
Log.e("BestProvider", bestProv);
|
64 |
|
|
|
65 |
|
|
if (bestProv != null) {
|
66 |
|
|
locManager.requestLocationUpdates(bestProv, 0, 0, this);
|
67 |
|
|
} else {
|
68 |
|
|
// message that no suitable provider was found
|
69 |
|
|
hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);
|
70 |
|
|
}
|
71 |
|
|
}
|
72 |
|
|
@Override
|
73 |
|
|
public void onLocationChanged(Location location) {
|
74 |
|
|
Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
|
75 |
|
|
|
76 |
|
|
if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix
|
77 |
|
|
return;
|
78 |
|
|
|
79 |
|
|
locManager.removeUpdates(this);
|
80 |
|
|
hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);
|
81 |
|
|
|
82 |
|
|
//ToDo: to be nice put the api key into the request
|
83 |
|
|
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";
|
84 |
|
|
//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";
|
85 |
|
|
|
86 |
torben |
240 |
try {
|
87 |
torben |
238 |
String data = DownloadUtil.getContent(urlSource, 30000, "UTF-8");
|
88 |
|
|
StringBuilder builder = new StringBuilder(data);
|
89 |
torben |
237 |
|
90 |
|
|
while (builder.charAt(0) != '{')
|
91 |
|
|
builder.deleteCharAt(0);
|
92 |
|
|
while (builder.charAt(builder.length()-1) != '}')
|
93 |
|
|
builder.deleteCharAt(builder.length()-1);
|
94 |
|
|
|
95 |
|
|
JSONObject json = new JSONObject(builder.toString());
|
96 |
|
|
// now have some fun with the results...
|
97 |
|
|
JSONArray res = json.getJSONArray("results");
|
98 |
|
|
|
99 |
|
|
Location tmpLocation = new Location("gps");
|
100 |
|
|
stationList.clear();
|
101 |
|
|
|
102 |
|
|
for (int i=0; i<res.length(); i++) {
|
103 |
|
|
JSONObject jsonStation = res.getJSONObject(i);
|
104 |
|
|
|
105 |
|
|
double lat = jsonStation.getDouble("lat");
|
106 |
|
|
double lng = jsonStation.getDouble("lng");
|
107 |
|
|
String title = jsonStation.getString("title");
|
108 |
|
|
String addr = jsonStation.getString("streetAddress");
|
109 |
|
|
tmpLocation.setLatitude(lat);
|
110 |
|
|
tmpLocation.setLongitude(lng);
|
111 |
|
|
|
112 |
|
|
float distance = location.distanceTo(tmpLocation);
|
113 |
|
|
|
114 |
|
|
stationList.add( new StationBean(title,lat,lng,(int)distance,addr) );
|
115 |
|
|
|
116 |
|
|
Log.e("Location", title + " " + lat +"," + lng);
|
117 |
|
|
}
|
118 |
|
|
hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
|
119 |
|
|
|
120 |
|
|
} catch (Exception e) {
|
121 |
|
|
Log.e("Location","Location",e);
|
122 |
|
|
hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
|
128 |
|
|
@Override
|
129 |
|
|
public void onProviderDisabled(String provider) {
|
130 |
|
|
// TODO Auto-generated method stub
|
131 |
|
|
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
@Override
|
136 |
|
|
public void onProviderEnabled(String provider) {
|
137 |
|
|
// TODO Auto-generated method stub
|
138 |
|
|
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
@Override
|
143 |
|
|
public void onStatusChanged(String provider, int status, Bundle extras) {
|
144 |
|
|
// TODO Auto-generated method stub
|
145 |
|
|
|
146 |
|
|
}
|
147 |
|
|
}
|