2

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();
            }
        });
Lukas Anda
  • 716
  • 1
  • 9
  • 30

2 Answers2

7

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();
                }
            });
Lukas Anda
  • 716
  • 1
  • 9
  • 30
0

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

Kunta Kinte
  • 351
  • 1
  • 2
  • 15
  • I thought about it like this but the problem is that the listener is triggered also during the movement. Therefore I will need to override somehow that listener with mine. – Lukas Anda May 11 '15 at 14:04
  • there are a very similar problem reported to google: https://code.google.com/p/gmaps-api-issues/issues/detail?id=4636 with no solution yet. In this threat there are some workarounds maybe one of this could be useful for you. – Kunta Kinte May 11 '15 at 14:16
  • Is there a way to put a timer there and updating only when the timer runs out ? – Lukas Anda May 11 '15 at 14:31
  • you could lauch a thread on oncamerachange and wait x milseconds in this thread. After waiting you can in this thread check if the position has changed from the position before waiting. And if it has not changed then is the end of the movement. Something like this. Not a very clean solution. – Kunta Kinte May 11 '15 at 14:36
  • Can you please give here some code ? Something with timer inside and after the timer runs out check. But I need to figure out how to handle the next call to the timer. – Lukas Anda May 11 '15 at 14:40
  • Try with something like this: http://developer.android.com/reference/android/os/CountDownTimer.html some better example http://stackoverflow.com/questions/17839419/android-thread-for-a-timer – Kunta Kinte May 11 '15 at 14:51