一如既往地获得新位置?我在 onCreate
here
locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled) {
if (location == null) {
provider = LocationManager.GPS_PROVIDER;
locationManager.requestLocationUpdates(provider, 500, 0, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latLng = new LatLng(location.getLatitude(), location.getLongitude());
if (location != null) {
ParseQueryMap();
}
}
}
}
}
if (isNetworkEnabled){
if (location == null) {
provider = LocationManager.NETWORK_PROVIDER;
locationManager.requestLocationUpdates(provider, 500, 0, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latLng = new LatLng(location.getLatitude(), location.getLongitude());
if (location != null) {
ParseQueryMap();
}
}
}
}
}
}
如果在我运行应用程序时该位置不可用,则采用以前已知的位置 (getLastKnownLocation()
)。我不喜欢这个解决方案。我需要应用程序立即或在一段时间后(大约 5 秒)向我显示我的确切位置。启动时的确切位置我需要显示最近的基地点。是否可以?感谢您的帮助
最佳答案
尝试使用被动位置提供程序。通过使用它,您可以接收位置更新以及其他已请求位置更新的应用。
参见 this blog entry其中详细描述了如何尽快修复该位置。您可以在 android-protips-location 中找到示例代码项目。
关于Android 总是在开始时得到一个新的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16037665/