/[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 474 - (hide annotations) (download)
Tue Oct 27 13:40:15 2009 UTC (14 years, 7 months ago) by torben
File size: 2498 byte(s)
Remove unneeded imports
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 241 Location savedLocation = null;
21 torben 286
22 torben 336 boolean isSearching = false;
23 torben 286 boolean hasGps;
24 torben 241
25 torben 319 public LocationLookup(Context c, Handler h) {
26 torben 237 cntx = c;
27     hndl = h;
28     }
29    
30 torben 285
31     public boolean hasLocation() {
32     return savedLocation != null;
33     }
34 torben 319
35     public Location getLocation()
36     {
37     return savedLocation;
38     }
39 torben 237
40     public void locateStations() {
41 torben 336
42     isSearching = true;
43    
44 torben 286 hasGps = false;
45 torben 237 locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
46 torben 286
47 torben 288 List<String> providers = locManager.getProviders(true);
48 torben 241
49 torben 286 if (providers.size() > 0) {
50     for(String provider : providers) {
51     Log.i("Provider", ""+provider);
52     if (provider.equalsIgnoreCase("gps"))
53     hasGps = true;
54     locManager.requestLocationUpdates(provider, 0, 0, this);
55     }
56 torben 237 } else {
57     // message that no suitable provider was found
58 torben 336 hndl.sendEmptyMessage(StationList.NOPROVIDER);
59 torben 237 }
60     }
61     @Override
62     public void onLocationChanged(Location location) {
63 torben 336 if (isSearching == false)
64     return;
65    
66 torben 237 Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
67 torben 336
68 torben 237
69 torben 286 if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
70     savedLocation = new Location(location); //save a copy
71 torben 241
72 torben 286 if (hasGps) {
73     if (!location.getProvider().equals("gps")) {
74     return; // at least give the gps a chance
75 torben 341 } else if (location.getAccuracy() > 256) {
76 torben 286 return; //if we have a gps provider lets wait for a more precise fix
77     }
78 torben 237
79 torben 286 }
80 torben 336 stopSearch();
81     hndl.sendEmptyMessage(StationList.GOTLOCATION);
82 torben 241 }
83 torben 336
84     public void stopSearch()
85     {
86     if (isSearching) {
87     isSearching = false;
88     locManager.removeUpdates(this);
89     }
90     }
91 torben 319
92 torben 237
93     @Override
94     public void onProviderDisabled(String provider) {
95     }
96    
97    
98     @Override
99     public void onProviderEnabled(String provider) {
100     }
101    
102    
103     @Override
104     public void onStatusChanged(String provider, int status, Bundle extras) {
105     // TODO Auto-generated method stub
106    
107     }
108     }

  ViewVC Help
Powered by ViewVC 1.1.20