繁体   English   中英

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

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

我正在制作一个使用谷歌地图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();
                }
            });

我认为你不能这样做“oncamerachange”会在动作结束时触发事件,但可能会在动作过程中触发或不触发其他人。

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

并且没有用于开始或结束相机移动的听众。

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

编辑从google gmaps-api-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;
    }

};

作者的评论:

嗨,我附上了一个anon listener包装实现的样本,你可以用CAMERA_MOVE_REACT_THRESHOLD_MS来评估你的最佳价值,如果它还不够,你也可以添加一些自动感知,这会使该阈值适应onCameraChange()通话频率

暂无
暂无

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

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