/[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 488 - (hide annotations) (download)
Thu Oct 29 13:42:57 2009 UTC (14 years, 7 months ago) by torben
File size: 2960 byte(s)
enable save of lastKnownLocation
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 488
61     saveLastKnownLocation(locManager.getLastKnownLocation(provider));
62 torben 286 }
63 torben 237 } else {
64     // message that no suitable provider was found
65 torben 336 hndl.sendEmptyMessage(StationList.NOPROVIDER);
66 torben 237 }
67     }
68     @Override
69     public void onLocationChanged(Location location) {
70 torben 336 if (isSearching == false)
71     return;
72    
73 torben 237 Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
74 torben 336
75 torben 237
76 torben 286 if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
77     savedLocation = new Location(location); //save a copy
78 torben 241
79 torben 286 if (hasGps) {
80     if (!location.getProvider().equals("gps")) {
81     return; // at least give the gps a chance
82 torben 341 } else if (location.getAccuracy() > 256) {
83 torben 286 return; //if we have a gps provider lets wait for a more precise fix
84     }
85 torben 237
86 torben 286 }
87 torben 336 stopSearch();
88     hndl.sendEmptyMessage(StationList.GOTLOCATION);
89 torben 241 }
90 torben 336
91 torben 488 private void saveLastKnownLocation(Location loc) {
92     if (lastKnownLocation == null) {
93     lastKnownLocation = loc;
94     } else {
95     if (loc.getTime() > lastKnownLocation.getTime()) {//if loc is more recent than saved
96     lastKnownLocation = loc;
97     }
98     }
99     }
100    
101 torben 336 public void stopSearch()
102     {
103     if (isSearching) {
104     isSearching = false;
105     locManager.removeUpdates(this);
106     }
107     }
108 torben 319
109 torben 237
110     @Override
111     public void onProviderDisabled(String provider) {
112     }
113    
114    
115     @Override
116     public void onProviderEnabled(String provider) {
117     }
118    
119    
120     @Override
121     public void onStatusChanged(String provider, int status, Bundle extras) {
122     // TODO Auto-generated method stub
123    
124     }
125     }

  ViewVC Help
Powered by ViewVC 1.1.20