百度地圖縮放 — 放開手指時地圖位置移動問題解決


一、問題分析

  我們在縮放地圖時,是想要在某一個位置進行縮放,但是在縮放結束時,由於手指不會同時離開不觸摸屏幕,所以會造成縮放結束后地圖會有一個移動的效果。這個效果是我們不需要的,這就是我們需要解決的問題。問題分析清楚就應該明白這個肯定是要從 Android 的事件傳遞機制入手的,我們怎么能讓地圖在縮放之后不響應最后的那個 MOVE (移動操作)?

二、 思路

(1)百度地圖有提供一個接口,我們可以實現這個接口去解決

BaiduMap.OnMapTouchListener

(2)在百度地圖上為它添加一個我們重寫了事件的 Parent View ,也可以在事件傳遞到地圖之前得到相應事件

  前面兩個思路經測試都已經解決了這個問題,但是我們需要注意在這兩種思路上並不能去攔截事件。如果不能保證將完整的事件攔截下來,就會打亂地圖對事件的響應。我們這里只是想讓地圖不移動,很開興百度地圖也為我們提供了一個方法:true 是可以移動地圖,false 是固定地圖,這個方式是實時生效的。

public void setScrollGesturesEnabled(boolean var1)

三、解決問題

  ① 思路一解決

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

  在布局中我們這么使用:

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

  以上就通過兩種思路解決了這個問題,如果有幫到你,---------------------------------------------------------------   我也很開心,哈哈! 下一篇我們將會實現一個效果,如何實現平滑的縮放地圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM