- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
您好,我正在创建一个可以获取用户位置的 Android map 应用程序,但是当用户打开他们的 map 时,有时位置不准确,并且会慢慢查明您的位置。
这个我试过了。但它仍然会调用处理程序,并且 TOASTS 不会停止。
private LocationManager locationManager;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps_page);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
handler.postDelayed(runnable,3000);
}
@Override
public void onProviderDisabled(String provider) {
handler.removeCallbacks(runnable);
}
});
private Runnable runnable = new Runnable() {
@Override
public void run() {
getCurrentLocation();
Toast.makeText(mapsPage.this, "GETTING LOCATION", Toast.LENGTH_SHORT).show();
handler.postDelayed(this, 3000);
}
};
private void getCurrentLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (location != null) {
longitude = location.getLongitude();
latitude = location.getLatitude();
moveMap();
Integer loc = Math.round(location.getAccuracy());
textings.setText(Integer.toString(loc));
if(loc <= 100)
{
handler.removeCallbacks(runnable);
Toast.makeText(mapsPage.this, "HANDLER STOPPED", Toast.LENGTH_SHORT).show();
}
}
}
//Function to move the map
private void moveMap() {
LatLng latLng = new LatLng(latitude, longitude);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(17));
mMap.addMarker(new MarkerOptions().position(latLng).draggable(false));
}
我添加了一个处理程序,它将每 5 秒运行一次以检查用户位置,当 getAccuracy 数据等于或小于 100 时它将停止。我该怎么做?
最佳答案
getCurrentLocation 可能会调用 handler.removeCallbacks(runnable);
,但是 runnable 总是会在之后调用 handler.postDelayed(this, 3000);
。
要解决这个问题,Runnable 必须有一些条件来检查它是否应该再次发布自己。
一个解决方案是让 getCurrentLocation 返回一个 bool 值,指示它是否成功(足够):
private boolean getCurrentLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return false;
}
Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (location != null) {
longitude = location.getLongitude();
latitude = location.getLatitude();
moveMap();
Integer loc = Math.round(location.getAccuracy());
textings.setText(Integer.toString(loc));
if(loc <= 100) {
handler.removeCallbacks(runnable);
Toast.makeText(mapsPage.this, "HANDLER STOPPED", Toast.LENGTH_SHORT).show();
return true;
}
}
return false;
}
然后在你的 Runnable 中检查你是否需要再运行一次:
@Override
public void run() {
if(!getCurrentLocation()) {
handler.postDelayed(this, 3000);
}
}
但是,综上所述,您应该只检查 LocationListener 的 onLocationChanged 中的 Location 并在该位置足够准确时执行某些操作。那么您根本不需要 Runnable。
关于android - 如何在 getAccuracy() 低于 100 时停止处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44143210/
我的应用程序中发生过以下崩溃,一次发生在我身上,一次发生在野外。当我在 map 周围画一个半径圆并且精度为负时,就会发生这种情况。除了硬编码负值外,我无法重现该问题。 在 Moto G gen1 (4
我正在构建一个基于位置的应用程序,我想避免使用这种位置:“Tel Aviv, Tel Aviv, Israel”,这是非常不准确的。 我在使用位置之前使用以下条件: if(!(this.locatio
我知道这个函数返回一个 float ,表示特定位置的准确性。我的问题是:这个函数返回的值越大是否意味着位置越准确?还是我说的正好相反? 最佳答案 数字越小,越准确。 关于android - Locat
我正在尝试使用 Location.getAccuracy() 获取我当前的 GPS 精度。但是,这似乎不可能返回 1(或有时返回 2)。 Google map 告诉我,我当前的定位精度是 40 米。
您好,我正在创建一个可以获取用户位置的 Android map 应用程序,但是当用户打开他们的 map 时,有时位置不准确,并且会慢慢查明您的位置。 这个我试过了。但它仍然会调用处理程序,并且 TOA
我想写一个LocationListener,它把最近最精确的位置作为它的位置。我打算在我的 LocationListener 中使用此代码: @Override public void
我是一名优秀的程序员,十分优秀!