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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 488 - (show annotations) (download)
Thu Oct 29 13:42:57 2009 UTC (14 years, 6 months ago) by torben
File size: 2960 byte(s)
enable save of lastKnownLocation
1 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 public class LocationLookup implements LocationListener{
15
16 LocationManager locManager;
17 Context cntx;
18 Handler hndl;
19
20 Location lastKnownLocation = null;
21 Location savedLocation = null;
22
23 boolean isSearching = false;
24 boolean hasGps;
25
26 public LocationLookup(Context c, Handler h) {
27 cntx = c;
28 hndl = h;
29 }
30
31
32 public boolean hasLocation() {
33 return savedLocation != null;
34 }
35
36 public Location getLocation()
37 {
38 return savedLocation;
39 }
40
41 public Location getLastKnownLocation() {
42 return lastKnownLocation;
43 }
44
45 public void locateStations() {
46
47 isSearching = true;
48
49 hasGps = false;
50 locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
51
52 List<String> providers = locManager.getProviders(true);
53
54 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
61 saveLastKnownLocation(locManager.getLastKnownLocation(provider));
62 }
63 } else {
64 // message that no suitable provider was found
65 hndl.sendEmptyMessage(StationList.NOPROVIDER);
66 }
67 }
68 @Override
69 public void onLocationChanged(Location location) {
70 if (isSearching == false)
71 return;
72
73 Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
74
75
76 if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
77 savedLocation = new Location(location); //save a copy
78
79 if (hasGps) {
80 if (!location.getProvider().equals("gps")) {
81 return; // at least give the gps a chance
82 } else if (location.getAccuracy() > 256) {
83 return; //if we have a gps provider lets wait for a more precise fix
84 }
85
86 }
87 stopSearch();
88 hndl.sendEmptyMessage(StationList.GOTLOCATION);
89 }
90
91 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 public void stopSearch()
102 {
103 if (isSearching) {
104 isSearching = false;
105 locManager.removeUpdates(this);
106 }
107 }
108
109
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