简体   繁体   English

当用户停止移动相机时,Android google maps v2会执行asynctask

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

I am making an app which is using google maps api v2. 我正在制作一个使用谷歌地图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. 我认为你不能这样做“oncamerachange”会在动作结束时触发事件,但可能会在动作过程中触发或不触发其他人。

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 . " Google文档:“在摄像机位置发生变化后调用。在动画期间,监听器可能无法通知中间摄像机位置。它总是在动画中调用结束位置。”

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. 做类似事情的一种方法是保存上次装入json的最后一点(latlang),每次有“oncamerachange”事件时,将当前点与前一点进行比较并确定是否要再次重新加载json。

Edited to post workaround from google gmaps-api-issues. 编辑从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 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 嗨,我附上了一个anon listener包装实现的样本,你可以用CAMERA_MOVE_REACT_THRESHOLD_MS来评估你的最佳价值,如果它还不够,你也可以添加一些自动感知,这会使该阈值适应onCameraChange()通话频率

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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