OnTouch in MapView only fires the first time

这一生的挚爱 提交于 2019-11-29 04:39:33

If you are using this method "mapView.setBuiltInZoomControls(true);" then your touch is working at once .

Please remove that that line and check I am sure it will work..

In some case if you want BuiltInZoomControls then you can you OnTouch method of Overlay like as below..

public class MapOverlay extends Overlay {

    public MapOverlay(Context ctx) {super(ctx);}

    @Override
    protected void draw(Canvas c, MapView osmv, boolean shadow) { }

    @Override
    public boolean onTouchEvent(MotionEvent e, MapView mapView) {
        //Write yout touch code here..
        return false;
    }
}
fhucho

I was having the same problem - I couldn't drag and scroll the map because it was receiving only ACTION_DOWN events. It can be solved by adding android:clickable="true" to the MapView or by calling mapView.setClickable(true).

Subclass MapView and override dispatchTouchEvent like below and use the subclass instead.

public class MyMap extends MapView{
@Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
         if (mOnMovedListener!= null) {
                mOnMovedListener.onMove(ev);
            }
            return super.dispatchTouchEvent(ev);
        }
    private OnMovedListener mOnMovedListener;

    public void setOnMovedListener(OnMovedListener mOnMovedListener) {
        this.mOnMovedListener= mOnMovedListener;
    }
}

register for listener like any other!

How about using a GestureDetector.OnDoubleTapListener instead of OnTouchListener?

Maxim

you should at least put

lasttime=event.getEventTime();

under the

if (event.getAction() == MotionEvent.ACTION_DOWN) brakes 

while onTouch detect ACTION_UP event of your click. So any time you make a click it is called 2 times

Implement the Touch event on the map view instead. That will work!!

    // The onTouch event of the Map itself does not fire!
    // We must implement it on the mapView!!!
    mapView.setOnTouchListener(new OnTouchListener()
    {
        public boolean onTouch(View v, MotionEvent event) 
        {
            // Your code and remember to return true!                

            return (true);
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!