咱們在縮放地圖時,是想要在某一個位置進行縮放,可是在縮放結束時,因爲手指不會同時離開不觸摸屏幕,因此會形成縮放結束後地圖會有一個移動的效果。這個效果是咱們不須要的,這就是咱們須要解決的問題。問題分析清楚就應該明白這個確定是要從 Android 的事件傳遞機制入手的,咱們怎麼能讓地圖在縮放以後不響應最後的那個 MOVE (移動操做)?html
(1)百度地圖有提供一個接口,咱們能夠實現這個接口去解決java
BaiduMap.OnMapTouchListener
(2)在百度地圖上爲它添加一個咱們重寫了事件的 Parent View ,也能夠在事件傳遞到地圖以前獲得相應事件android
前面兩個思路經測試都已經解決了這個問題,可是咱們須要注意在這兩種思路上並不能去攔截事件。若是不能保證將完整的事件攔截下來,就會打亂地圖對事件的響應。咱們這裏只是想讓地圖不移動,很開興百度地圖也爲咱們提供了一個方法:true 是能夠移動地圖,false 是固定地圖,這個方式是實時生效的。api
public void setScrollGesturesEnabled(boolean var1)
① 思路一解決ide
baiduMap.setOnMapTouchListener { val pointerCount = it.pointerCount when (it.action) { MotionEvent.ACTION_DOWN -> { mapView.map.uiSettings.isScrollGesturesEnabled = true } MotionEvent.ACTION_MOVE -> {
//pointerCount 操做的手指個數 if (pointerCount >= 2) { mapView.map.uiSettings.isScrollGesturesEnabled = false } } MotionEvent.ACTION_UP -> { handler.postDelayed({ mapView.map.uiSettings.isScrollGesturesEnabled = true }, 500) } } }
② 思路二解決: 看代碼咱們是自定義了一個 ViewGroup佈局
class MapOverlayLayout(context: Context?, attrs: AttributeSet?) : RelativeLayout(context, attrs) { private var mMapView: MapView? = null private var isMutilPoint = false private var isOnePoint = true override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { super.onLayout(changed, l, t, r, b) if (changed) setup() } private fun setup() { if (mMapView != null) return for (i in 0 until childCount) { val child = getChildAt(i) if (child != null && child is MapView) { mMapView = child break } } if (childCount > 0 && mMapView == null) Log.e(this.javaClass.simpleName, "未將地圖放置在子節點下") } fun setBaiduMap(mapView: MapView) { mMapView = mapView } override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean { var isIntercept = false when (ev!!.action) { MotionEvent.ACTION_MOVE -> { mMapView!!.map.uiSettings.isScrollGesturesEnabled = !isMutilPoint && isOnePoint if (ev.pointerCount >= 2) { isMutilPoint = true } if (ev.pointerCount == 1) { isOnePoint = true } } MotionEvent.ACTION_UP -> { isMutilPoint = false isOnePoint = true } } return isIntercept } }
在佈局中咱們這麼使用:post
<com.main.map.MapOverlayLayout android:id="@+id/mark_container" android:layout_width="match_parent" android:layout_height="match_parent"> <com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:focusable="true" /> </com.main.map.MapOverlayLayout>
以上就經過兩種思路解決了這個問題,若是有幫到你,--------------------------------------------------------------- 我也很開心,哈哈! 下一篇咱們將會實現一個效果,如何實現平滑的縮放地圖測試