我需要从网络和GPS提供商接收位置更改。Android位置管理器标准
如果GPS提供商不能提供或没有位置(卫星可见度差),我会从GPS提供商的其他网络提供商处获得位置。
是否可以根据我的需要选择使用标准的提供商?
我需要从网络和GPS提供商接收位置更改。Android位置管理器标准
如果GPS提供商不能提供或没有位置(卫星可见度差),我会从GPS提供商的其他网络提供商处获得位置。
是否可以根据我的需要选择使用标准的提供商?
其实Android Developers - Making Your App Location Aware有一个很好的例子代码,以满足您的需求。
在其代码,如果你同时使用供应商(根据GPS和网络),它会做一个比较:
...
} else if (mUseBoth) {
// Get coarse and fine location updates.
mFineProviderButton.setBackgroundResource(R.drawable.button_inactive);
mBothProviderButton.setBackgroundResource(R.drawable.button_active);
// Request updates from both fine (gps) and coarse (network) providers.
gpsLocation = requestUpdatesFromProvider(
LocationManager.GPS_PROVIDER, R.string.not_support_gps);
networkLocation = requestUpdatesFromProvider(
LocationManager.NETWORK_PROVIDER, R.string.not_support_network);
// If both providers return last known locations, compare the two and use the better
// one to update the UI. If only one provider returns a location, use it.
if (gpsLocation != null && networkLocation != null) {
updateUILocation(getBetterLocation(gpsLocation, networkLocation));
} else if (gpsLocation != null) {
updateUILocation(gpsLocation);
} else if (networkLocation != null) {
updateUILocation(networkLocation);
}
}
...
它实施最佳位置提供的想法(通过在指定期间内判断的准确像时间:
/** Determines whether one Location reading is better than the current Location fix.
* Code taken from
* http://developer.android.com/guide/topics/location/obtaining-user-location.html
*
* @param newLocation The new Location that you want to evaluate
* @param currentBestLocation The current Location fix, to which you want to compare the new
* one
* @return The better Location object based on recency and accuracy.
*/
protected Location getBetterLocation(Location newLocation, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return newLocation;
}
// Check whether the new location fix is newer or older
long timeDelta = newLocation.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 newLocation;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) {
return currentBestLocation;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (newLocation.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(newLocation.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return newLocation;
} else if (isNewer && !isLessAccurate) {
return newLocation;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return newLocation;
}
return currentBestLocation;
}
有关完整代码,请看看上面提供的链接
只要不放弃任何的标准,并设法得到最好提供商:
LocationManager mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String best = mgr.getBestProvider(criteria, true);
//since you are using true as the second parameter, you will only get the best of providers which are enabled.
Location location = mgr.getLastKnownLocation(best);
这不起作用 – silverFoxA 2015-05-28 17:12:31
对于搜索最佳位置提供商请用户belowe℃。 ode: 请参阅https://developer.android.com/reference/android/location/Criteria.html 了解,标准将如何工作?
public String getProviderName() {
LocationManager locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setSpeedRequired(true);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
return locationManager.getBestProvider(criteria, true);
}
如果您使用Criteria类来根据配置查找最佳提供商,那么将会导致来自GPS提供商,网络提供商或被动方的任何人。并没有标准类使用requestLocationUpdates获取位置代码提供商 – 2013-03-28 10:20:40
LocationManager不会通知我有关网络和GPS都在一个回调?需要我使用两个requestLocation()与不同的回调? – Nik 2013-03-28 10:25:39
要注册您的位置管理器以获取更新,您必须提供您想要获取更新的提供商。像'mgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,10 * 1000,50,this);' – 2013-03-28 10:30:31