当用户停止移动相机时,Android google maps v2 执行异步任务

标签 android google-maps android-asynctask

我正在制作一个使用 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/

相关文章:

android - Android错误日志, list 中的 Activity ,FATAL EXCEPTION : AsyncTask #1

java - 当我尝试从 Intent putExtra 时,出现空指针异常

java - 将所有常量放在一个类android中

javascript - 为什么这个函数调用会破坏程序并导致 undefined variable ?

android - 为什么我使用 AsyncTask 时会出现 android.os.NetworkOnMainThreadException?

java - 无法在 asyncTask 的 onPostExecute 中将 json 字符串设置为 TextView

Android AlertDialog标题背景色

android - 使用 Firestore 和 Google Speech to Text 时出现重复的类错误

google-maps - 自昨晚以来,Google map 信息窗口变小

c# - 在 Pin Map 控件 Xamarin 表单 pcl 上调用单击事件