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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 948 - (hide annotations) (download)
Fri Jul 2 14:58:44 2010 UTC (13 years, 10 months ago) by torben
File size: 3038 byte(s)
Be a little more large on gps precission, but don't wait as long
1 torben 237 package dk.thoerup.traininfo;
2    
3     import java.util.List;
4    
5     import android.content.Context;
6     import android.location.Location;
7     import android.location.LocationListener;
8     import android.location.LocationManager;
9     import android.os.Bundle;
10     import android.os.Handler;
11     import android.util.Log;
12    
13    
14 torben 319 public class LocationLookup implements LocationListener{
15    
16 torben 237 LocationManager locManager;
17     Context cntx;
18     Handler hndl;
19    
20 torben 488 Location lastKnownLocation = null;
21 torben 241 Location savedLocation = null;
22 torben 286
23 torben 336 boolean isSearching = false;
24 torben 286 boolean hasGps;
25 torben 241
26 torben 319 public LocationLookup(Context c, Handler h) {
27 torben 237 cntx = c;
28     hndl = h;
29     }
30    
31 torben 285
32     public boolean hasLocation() {
33     return savedLocation != null;
34     }
35 torben 319
36     public Location getLocation()
37     {
38     return savedLocation;
39     }
40 torben 488
41     public Location getLastKnownLocation() {
42     return lastKnownLocation;
43     }
44 torben 237
45     public void locateStations() {
46 torben 336
47     isSearching = true;
48    
49 torben 286 hasGps = false;
50 torben 237 locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
51 torben 286
52 torben 288 List<String> providers = locManager.getProviders(true);
53 torben 241
54 torben 286 if (providers.size() > 0) {
55     for(String provider : providers) {
56     Log.i("Provider", ""+provider);
57     if (provider.equalsIgnoreCase("gps"))
58     hasGps = true;
59     locManager.requestLocationUpdates(provider, 0, 0, this);
60 torben 570 Location tmpLastKnown = locManager.getLastKnownLocation(provider);
61     if (tmpLastKnown != null) {
62     saveLastKnownLocation(tmpLastKnown);
63     }
64 torben 286 }
65 torben 237 } else {
66     // message that no suitable provider was found
67 torben 336 hndl.sendEmptyMessage(StationList.NOPROVIDER);
68 torben 237 }
69     }
70     @Override
71     public void onLocationChanged(Location location) {
72 torben 336 if (isSearching == false)
73     return;
74    
75 torben 237 Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
76 torben 336
77 torben 237
78 torben 286 if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
79     savedLocation = new Location(location); //save a copy
80 torben 241
81 torben 286 if (hasGps) {
82     if (!location.getProvider().equals("gps")) {
83     return; // at least give the gps a chance
84 torben 948 } else if (location.getAccuracy() > 512) {
85 torben 286 return; //if we have a gps provider lets wait for a more precise fix
86     }
87 torben 237
88 torben 286 }
89 torben 336 stopSearch();
90     hndl.sendEmptyMessage(StationList.GOTLOCATION);
91 torben 241 }
92 torben 336
93 torben 488 private void saveLastKnownLocation(Location loc) {
94     if (lastKnownLocation == null) {
95     lastKnownLocation = loc;
96     } else {
97     if (loc.getTime() > lastKnownLocation.getTime()) {//if loc is more recent than saved
98     lastKnownLocation = loc;
99     }
100     }
101     }
102    
103 torben 336 public void stopSearch()
104     {
105     if (isSearching) {
106     isSearching = false;
107     locManager.removeUpdates(this);
108     }
109     }
110 torben 319
111 torben 237
112     @Override
113     public void onProviderDisabled(String provider) {
114     }
115    
116    
117     @Override
118     public void onProviderEnabled(String provider) {
119     }
120    
121    
122     @Override
123     public void onStatusChanged(String provider, int status, Bundle extras) {
124     // TODO Auto-generated method stub
125    
126     }
127     }

  ViewVC Help
Powered by ViewVC 1.1.20