2017-03-27 72 views
1

是否有任何選項谷歌地圖android類似於iOS GoogleMap.settings.allowScrollGesturesDuringRotateOrZoom?禁用Mapview滾動縮放android

基本上我想實現的是保持標記在地圖中心的相對佈局。

+0

請明星此功能的要求,讓谷歌知道我們需要它︰https://issuetracker.google.com/issues/69795937 – dan

回答

0

您可以參考此thread。雖然因爲ScaleGestureDetector錯誤的,將工作僅API> 16.定期Mapview與API啓用規模比劃< = 16和API> 16 Reference.

public class CustomEventMapView extends MapView { 

    private int fingers = 0; 
    private GoogleMap googleMap; 
    private long lastZoomTime = 0; 
    private float lastSpan = -1; 
    private Handler handler = new Handler(); 

    private ScaleGestureDetector gestureDetector; 

    public CustomEventMapView(Context context, GoogleMapOptions options) { 
     super(context, options); 
    } 

    public CustomEventMapView(Context context) { 
     super(context); 
    } 

    @Override 
    public void getMapAsync(final OnMapReadyCallback callback) { 
     super.getMapAsync(new OnMapReadyCallback() { 
      @Override 
      public void onMapReady(final GoogleMap googleMap) { 
       gestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.OnScaleGestureListener() { 
        @Override 
        public boolean onScale(ScaleGestureDetector detector) { 
         if (lastSpan == -1) { 
          lastSpan = detector.getCurrentSpan(); 
         } else if (detector.getEventTime() - lastZoomTime >= 50) { 
          lastZoomTime = detector.getEventTime(); 
          googleMap.animateCamera(CameraUpdateFactory.zoomBy(getZoomValue(detector.getCurrentSpan(), lastSpan)), 50, null); 
          lastSpan = detector.getCurrentSpan(); 
         } 
         return false; 
        } 

        @Override 
        public boolean onScaleBegin(ScaleGestureDetector detector) { 
         lastSpan = -1; 
         return true; 
        } 

        @Override 
        public void onScaleEnd(ScaleGestureDetector detector) { 
         lastSpan = -1; 

        } 
       }); 
       CustomEventMapView.this.googleMap = googleMap; 
       callback.onMapReady(googleMap); 
      } 
     }); 
    } 

    private float getZoomValue(float currentSpan, float lastSpan) { 
     double value = (Math.log(currentSpan/lastSpan)/Math.log(1.55d)); 
     return (float) value; 
    } 

    @Override 
    public boolean dispatchTouchEvent(MotionEvent ev) { 

     switch (ev.getAction() & MotionEvent.ACTION_MASK) { 
      case MotionEvent.ACTION_POINTER_DOWN: 
       fingers = fingers + 1; 
       break; 
      case MotionEvent.ACTION_POINTER_UP: 
       fingers = fingers - 1; 
       break; 
      case MotionEvent.ACTION_UP: 
       fingers = 0; 
       break; 
      case MotionEvent.ACTION_DOWN: 
       fingers = 1; 
       break; 
     } 
     if (fingers > 1) { 
      disableScrolling(); 
     } else if (fingers < 1) { 
      enableScrolling(); 
     } 
     if (fingers > 1) { 
      return gestureDetector.onTouchEvent(ev); 
     } else { 
      return super.dispatchTouchEvent(ev); 
     } 
    } 

    private void enableScrolling() { 
     if (googleMap != null && !googleMap.getUiSettings().isScrollGesturesEnabled()) { 
      handler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        googleMap.getUiSettings().setAllGesturesEnabled(true); 
       } 
      }, 50); 
     } 
    } 

    private void disableScrolling() { 
     handler.removeCallbacksAndMessages(null); 
     if (googleMap != null && googleMap.getUiSettings().isScrollGesturesEnabled()) { 
      googleMap.getUiSettings().setAllGesturesEnabled(false); 
     } 
    } 
} 

而且,這裏有一個相關的線程這可能幫助:MapView - Disable dragging around, but allow zooming

+0

謝謝你的回覆... 但該應用程序崩潰使用mapview的實現由您提供。 引發:java.lang.NoSuchMethodException: [class android.content.Context,interface android.util.AttributeSet] –