简体   繁体   中英

Android google maps v2 execute asynctask when user stops moving the camera

I am making an app which is using google maps api v2. My problem is that when I register camera change listener, it executes each time the camera moves not when the moving stops. I want to achieve the latter. How can I modify my code to register only when moving stops ?

Here is my code:

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();
            }
        });

I implemented custom timer to execute after certain time only when conditions are met. Here is my code:

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();
                }
            });

I think you can not do this "oncamerachange" triggers an event at the end of the movement, but may trigger, or not, others during the movement.

Google docs : " Called after the camera position has changed . During an animation , listener May not be Notified of intermediate camera positions . It is always called for the end position in the animation . "

And there are no listeners for start or end camera movement.

One way to do something similar, is to save the last point ( latlang ) where you loaded the json for the last time and every time there is a " oncamerachange " event, compare the current point to the previous point and decide if you want to reload the json again or not.

Edited to post workaround from google gmaps-api-issues.

Maybe this is what are you loocking for. From the discusion on google issues.

https://code.google.com/p/gmaps-api-issues/issues/attachmentText?id=4636&aid=46360026000&name=sample_inner_listener.java&token=ABZ6GAdaQesCaWQ_IU8t9J222IBYzPceUA%3A1431366784956

new GoogleMap.OnCameraChangeListener() {
    private static int CAMERA_MOVE_REACT_THRESHOLD_MS = 500;
    private long lastCallMs = Long.MIN_VALUE;

    @Override
    public void onCameraChange(CameraPosition cameraPosition) {
     final long snap = System.currentTimeMillis();
     if (lastCallMs + CAMERA_MOVE_REACT_THRESHOLD_MS > snap) {
       lastCallMs = snap;
       return;
     }

     ;; // here the actual call whatever you want to do on camera change

     lastCallMs = snap;
    }

};

Comment from the author:

hi, I've attached a sample of anon listener wrapper implementation, you can play with CAMERA_MOVE_REACT_THRESHOLD_MS to evaluate best value for you, if its not enough, you could also add kinda autosensing though, which would adapt that threshold value to the onCameraChange() call frequency

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM