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

Annotation of /android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 237 - (hide annotations) (download)
Sat Aug 8 19:02:20 2009 UTC (14 years, 9 months ago) by torben
File size: 5040 byte(s)
First version
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     import android.content.Context;
14     import android.location.Criteria;
15     import android.location.Location;
16     import android.location.LocationListener;
17     import android.location.LocationManager;
18     import android.os.Bundle;
19     import android.os.Handler;
20     import android.util.Log;
21    
22     public class StationLocator implements LocationListener{
23    
24     LocationManager locManager;
25     Context cntx;
26     Handler hndl;
27    
28     ArrayList<StationBean> stationList = new ArrayList<StationBean>();
29     List<StationBean> safeStationList = java.util.Collections.unmodifiableList(stationList);
30    
31     public StationLocator(Context c, Handler h) {
32     cntx = c;
33     hndl = h;
34     }
35    
36     public List<StationBean> getStations() {
37     return safeStationList;
38     }
39    
40     public void abortLocationListener() {
41     locManager.removeUpdates(this);
42     }
43    
44     public void locateStations() {
45     //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
46    
47     locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
48     /*//testcode
49     List<String> provs = locManager.getAllProviders();
50     for (String p : provs) {
51     Log.e("Provider", p);
52     }
53     provs = locManager.getProviders(true);
54     for (String p : provs) {
55     Log.e("ActiveProvider", p);
56     }
57     */
58     Criteria c = new Criteria();
59     c.setAccuracy(Criteria.ACCURACY_FINE);
60     String bestProv = locManager.getBestProvider(c, true);
61     Log.e("BestProvider", bestProv);
62    
63     if (bestProv != null) {
64     locManager.requestLocationUpdates(bestProv, 0, 0, this);
65     } else {
66     // message that no suitable provider was found
67     hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);
68     }
69     }
70     @Override
71     public void onLocationChanged(Location location) {
72     Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
73    
74     if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix
75     return;
76    
77     locManager.removeUpdates(this);
78     hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);
79    
80     //ToDo: to be nice put the api key into the request
81     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";
82     //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";
83    
84     try {
85     Log.e("Url", urlSource);
86     URL url = new URL(urlSource);
87     URLConnection connection = url.openConnection();
88     connection.setConnectTimeout(5000);
89    
90     String line;
91     StringBuilder builder = new StringBuilder();
92     BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()), 8192);
93     while((line = reader.readLine()) != null) {
94     builder.append(line);
95     }
96    
97     while (builder.charAt(0) != '{')
98     builder.deleteCharAt(0);
99     while (builder.charAt(builder.length()-1) != '}')
100     builder.deleteCharAt(builder.length()-1);
101    
102     JSONObject json = new JSONObject(builder.toString());
103     // now have some fun with the results...
104     JSONArray res = json.getJSONArray("results");
105    
106     Location tmpLocation = new Location("gps");
107     stationList.clear();
108    
109     for (int i=0; i<res.length(); i++) {
110     JSONObject jsonStation = res.getJSONObject(i);
111    
112     double lat = jsonStation.getDouble("lat");
113     double lng = jsonStation.getDouble("lng");
114     String title = jsonStation.getString("title");
115     String addr = jsonStation.getString("streetAddress");
116     tmpLocation.setLatitude(lat);
117     tmpLocation.setLongitude(lng);
118    
119     float distance = location.distanceTo(tmpLocation);
120    
121     stationList.add( new StationBean(title,lat,lng,(int)distance,addr) );
122    
123     Log.e("Location", title + " " + lat +"," + lng);
124     }
125     hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
126    
127     } catch (Exception e) {
128     Log.e("Location","Location",e);
129     hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
130     }
131    
132     }
133    
134    
135     @Override
136     public void onProviderDisabled(String provider) {
137     // TODO Auto-generated method stub
138    
139     }
140    
141    
142     @Override
143     public void onProviderEnabled(String provider) {
144     // TODO Auto-generated method stub
145    
146     }
147    
148    
149     @Override
150     public void onStatusChanged(String provider, int status, Bundle extras) {
151     // TODO Auto-generated method stub
152    
153     }
154     }

  ViewVC Help
Powered by ViewVC 1.1.20