1 2 3 4 5 |
//變量定義 private LocationManager locationManager; //獲得LocationManager locationManager = (LocationManager) this .getSystemService(Context.LOCATION_SERVICE) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
private class MyLocationListner implements LocationListener{ @Override public void onLocationChanged(Location location) { // Called when a new location is found by the location provider. Log.v("GPSTEST", "Got New Location of provider:"+location.getProvider()); if(currentLocation!=null){ if(isBetterLocation(location, currentLocation)){ Log.v("GPSTEST", "It's a better location"); currentLocation=location; showLocation(location); } else{ Log.v("GPSTEST", "Not very good!"); } } else{ Log.v("GPSTEST", "It's first location"); currentLocation=location; showLocation(location); } //移除基於LocationManager.NETWORK_PROVIDER的監聽器 if(LocationManager.NETWORK_PROVIDER.equals(location.getProvider())){ locationManager.removeUpdates(this); } } //後3個方法此處不作處理 public void onStatusChanged(String provider, int status, Bundle extras) { } public void onProviderEnabled(String provider) { } public void onProviderDisabled(String provider) { } }; Location currentLocation; private void showLocation(Location location){ //緯度 Log.v("GPSTEST","Latitude:"+location.getLatitude()); //經度 Log.v("GPSTEST","Longitude:+location.getLongitude()); //精確度 Log.v("GPSTEST","Accuracy:"+location.getAccuracy()); //Location還有其它屬性,請自行探索 } |
1 2 3 4 5 6 7 8 |
private LocationListener gpsListener=null; private LocationListener networkListner=null; private void registerLocationListener(){ networkListner=new MyLocationListner(); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 0, networkListner); gpsListener=new MyLocationListner(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, gpsListener); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
private static final int CHECK_INTERVAL = 1000 * 30; protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > CHECK_INTERVAL; boolean isSignificantlyOlder = timeDelta < -CHECK_INTERVAL; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, // use the new location // because the user has likely moved if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must // be worse } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation .getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location are from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and // accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers are the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } |
1 2 3 4 |
if(gpsListener!=null){ locationManager.removeUpdates(gpsListener); gpsListener=null; } |