2015-05-11 19 views
2

我正在製作一個應用程序,該應用程序正在使用谷歌地圖api v2。我的問題是,當我註冊相機更改偵聽器時,它會在每次移動相機時移動,而不是在移動停止時執行。我想實現後者。我怎樣才能修改我的代碼只在移動停止時註冊?Android谷歌地圖v2在用戶停止移動攝像頭時執行asynctask

這裏是我的代碼:

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

http://stackoverflow.com/questions/13702117/how-can-i-handle-map-move-end-using-google-maps-for-android-v2 – Carnal

+0

的代碼不完整。難以理解 – Traabefi

回答

6

我實現了自定義計時器執行一定時間後才滿足條件。這裏是我的代碼:

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

我認爲你不能做到這一點「oncamerachange」觸發在運動結束的事件,但可能會引發,還是不行,其他人在運動過程中。

谷歌文檔: 「攝像機位置後調用已經改變了在一個動畫,聽衆可能不會通知中間攝影機位置的它總是要求在動畫的結束位置。」

還有不是開始或結束相機移動的聽衆。

一種做類似的方法是保存最後一次加載json的最後一個點(latlang),並且每次發生「oncamerachange」事件時,將當前點與前一點進行比較並決定如果你想重新加載json或不。

被編輯爲從google gmaps-api-issues發佈解決方法。

也許這是你爲什麼loocking。從谷歌問題的謠言。

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

};從筆者

評論:

嗨,我已經把它貼匿名聽衆包裝實施的樣本,你可以玩CAMERA_MOVE_REACT_THRESHOLD_MS爲您評估最有價值的,如果不夠的話,還可以添加有點autosensing雖然,這將適應該閾值onCameraChange()調用頻率

+0

我這樣想過,但問題在於在移動過程中也會觸發聽衆。因此,我需要用我的方式來重寫那個監聽器。 – Traabefi

+0

有一個非常類似的問題報告給谷歌:https://code.google.com/p/gmaps-api-issues/issues/detail?id=4636目前還沒有解決方案。在這種威脅中,有一些解決方法可能對您有用。 –

+0

有沒有辦法在那裏放置一個計時器,只有在計時器用完時才更新? – Traabefi