簡體   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