我正在制作一个使用 google maps api v2 的应用程序。我的问题是,当我注册相机更改监听器时,它会在每次相机移动时执行,而不是在移动停止时执行。我想实现后者。如何修改我的代码以仅在移动停止时注册?
这是我的代码:
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition position) {
LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
ne = bounds.northeast;
sw = bounds.southwest;
ne1 = ne.latitude;
ne2 = ne.longitude;
sw1 = sw.latitude;
sw2 = sw.longitude;
new DownloadJSON().execute();
}
});
最佳答案
我实现了自定义计时器,仅在满足条件时在特定时间后执行。这是我的代码:
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition position) {
LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
ne = bounds.northeast;
sw = bounds.southwest;
if(t!=null){
t.purge();
t.cancel();
}
t = new Timer();
t.schedule(new TimerTask() {
public void run() {
if(ne1 != ne.latitude && ne2 != ne.longitude && sw1 != sw.latitude && sw2 != sw.longitude){
ne1 = ne.latitude;
ne2 = ne.longitude;
sw1 = sw.latitude;
sw2 = sw.longitude;
Log.d("Tag","Refreshing data");
new DownloadJSON().execute();
t.cancel();
}
else{
ne1 = ne.latitude;
ne2 = ne.longitude;
sw1 = sw.latitude;
sw2 = sw.longitude;}
t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
}
}, 1000);
//new DownloadJSON().execute();
}
});
关于当用户停止移动相机时,Android google maps v2 执行异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30162835/