我想在确定设备位置时显示一个对话框。我还想同时使用 wifi 和 gps 提供商。当位置发生变化时,应更新纬度和经度值。我找了一个例子,但我没有找到任何东西。
问问题
1682 次
1 回答
1
这是一个如何定义位置管理器并获取位置更改事件的示例:
public class LocationChangeListener implements LocationListener{
private LocationManager mLocationManager = null;
private static final int TWO_MINUTES = 1000 * 60 * 2;
private Location currentBestLocation = null;
public LocationChangeListener(Context context) {
super(context);
}
public void impl(int refreshPeriod, int accuracyInMeters){
// Get the location manager
mLocationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
currentBestLocation = getLastBestLocation();
boolean enabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// Check if enabled and if not send user to the GSP settings
// Better solution would be to display a dialog and suggesting to
// go to the settings
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
Criteria criteria = new Criteria();
//criteria.setAccuracy(Criteria.ACCURACY_FINE);
//criteria.setCostAllowed(false);
String provider = mLocationManager.getBestProvider(criteria, false);
Location location = mLocationManager.getLastKnownLocation(provider);
LocationListener listener = this;
mLocationManager.requestLocationUpdates(provider, refreshPeriod, accuracyInMeters, listener);
//mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
//mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
this.onLocationChanged(location);
// Initialize the location fields
if (location == null) {
//Location not available
mLocationManager.removeUpdates(this);
}
}
/**
* This method returns the last know location, between the GPS and the Network one.
* For this method newer is best :)
*
* @return the last know best location
*/
private Location getLastBestLocation() {
Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
long GPSLocationTime = 0;
if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); }
long NetLocationTime = 0;
if (null != locationNet) {
NetLocationTime = locationNet.getTime();
}
if ( 0 < GPSLocationTime - NetLocationTime ) {
return locationGPS;
}
else{
return locationNet;
}
}
/**
* This method modify the last know good location according to the arguments.
*
* @param location the possible new location
*/
void makeUseOfNewLocation(Location location) {
if ( isBetterLocation(location, currentBestLocation) ) {
currentBestLocation = location;
}
}
public void onLocationChanged(Location location) {
makeUseOfNewLocation(location);
if(currentBestLocation == null){
currentBestLocation = location;
}
FlowController.isTriggerEventReceived = true;
mCobytLocationCallback.onLocationChanged(currentBestLocation);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
/** Determines whether one Location reading is better than the current Location fix
* @param location The new Location that you want to evaluate
* @param currentBestLocation The current Location fix, to which you want to compare the new one
*/
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 > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
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);
}
public void destroyManager() {
if(mLocationManager != null){
mLocationManager.removeUpdates(this);
mLocationManager = null;
}
}
}
由于安卓有
GPS_PROVIDER
您可以同时注册两者并从 2NETWORK_PROVIDER
开始获取事件。onLocationChanged(Location location)
到现在为止还挺好。现在的问题是我们需要 2 个结果还是我们应该采取最好的。据我所知NETWORK PROVIDER
,结果比GPS
.
在我们开始监听位置变化之前,我们将实现跟随方法private Location getLastBestLocation()
。此方法返回 GPS 和网络之间的最后一个已知位置。对于这种方法,更新的最好。
于 2012-10-23T12:36:12.667 回答