一如既往地获得新的位置?我在onCreate
这里使用此代码
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 秒)向我显示我的确切位置。启动时的确切位置我需要显示基地的最近点。可能吗?感谢您的任何帮助