關於 android 中的MapView 雙擊事件的問題?

來源:互聯網
上載者:User

我在做的程式的時候,遇到這樣的一個問題,就是,雙擊mapView響應事件,後來試了很多方法,後來終於成功了!

這是我從網上搜集的一些資料。如下:

touch與多個mapview 的屬性及傳回值的總結

 序號

 setClickable

 setEnabled

 onTouch函數

傳回值

 結果

 1  預設  預設  false  僅響應第一次,mapview正常響應
 2  預設  預設  true  每次都響應,mapview不能正常響應
 3  true   true  false  僅響應第一次,mapview正常響應
 4   true   true  true  每次都響應,mapview不能正常響應
         
         
         
         
         

根據以上調查結果得出結論

onTouch函數的傳回值直接影響著onTouch函數響應情況,mapview和onTouch函數響應互斥,與setClickable setEnabled無關

 

決定使用dispatchTouchEvent函數,下面是我重寫的dispatchTouch方法,主要功能是,雙擊mapView能自動放大地圖。以供大家參考!

首先這個執行個體只能用在4.0以上版本

  你想點擊任意一點獲得座標不能用onTouchEvent,而只能用dispatchTouchEvent 因為“MapActivity 繼承了 the dispatchTouch Event 而不是OnTouchEvent ”

  @Override
 public boolean dispatchTouchEvent(MotionEvent ev) {
     int actionType=ev.getAction();
  switch(actionType){
  case MotionEvent.ACTION_DOWN:
      count++;
   if(count==1){
     firClick=System.currentTimeMillis();
   }else if(count==2){
    //Toast.makeText(ActivityMain.this, "aaa", Toast.LENGTH_SHORT).show();
    secClick=System.currentTimeMillis();
    System.out.println("sec:"+secClick);
    System.out.println("first:"+firClick);
    distanceTime=secClick-firClick;
    System.out.println("distanceTime:"+distanceTime);
    if(distanceTime>1 && distanceTime<1000){
     Toast.makeText(ActivityMain.this, "aaa", Toast.LENGTH_SHORT).show();
    intZoomLevel++;
          if (intZoomLevel > mMapView01.getMaxZoomLevel()) {
           intZoomLevel = mMapView01.getMaxZoomLevel();
           }
          mMapController01.setZoom(intZoomLevel);
    }
    firClick=0;
    secClick=0;
    count=0;
    
   }
  }
  return super.dispatchTouchEvent(ev);
 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.