GestureListener只是第一次反应

时间:2013-10-08 20:09:04

标签: java android gesture ontouchevent

我收到一个奇怪的错误。 我想要做的是从mapview读取长触摸坐标并在该坐标上添加Waypoint。代码如下所示:

package fragments;

import java.util.zip.Inflater;

import org.osmdroid.api.IProjection;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.MapView.Projection;

import android.content.Intent;
import android.gesture.GestureLibraries;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import com.gps.gpsclient.DBAccessManager;
import com.gps.gpsclient.MapDrawer;
import com.gps.gpsclient.R;
import android.view.GestureDetector;


public class MapFragment extends Fragment implements 
GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener
{
    private MapView view;
    private MapDrawer mainDrawer;
    private GestureDetector gestureScanner;
    private MapController mapController;

    public MapFragment()
    {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.map_fragment_layout, container, false);

            view = (MapView) rootView.findViewById(R.id.fragmentMapView);
            view.setTileSource(TileSourceFactory.MAPNIK);
            view.setBuiltInZoomControls(true);
            view.setAlwaysDrawnWithCacheEnabled(true);
            mapController = view.getController();
            mapController.setZoom(10);
            GeoPoint point2 = new GeoPoint(51496994, -134733);
            mapController.setCenter(point2);
            mainDrawer=new MapDrawer(view);
            gestureScanner=new GestureDetector(view.getContext(),this);

            view.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                   gestureScanner.onTouchEvent(event);
                    return false;

                }

            });





        return rootView;
    }
    @Override
    public void onViewStateRestored(Bundle savedInstance)
    {
        super.onViewStateRestored(savedInstance);
    }

    @Override
    public boolean onDoubleTap(MotionEvent arg0) {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public boolean onDoubleTapEvent(MotionEvent arg0) {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public boolean onDown(MotionEvent arg0) {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public void onLongPress(MotionEvent e)
    {
        Projection projection=view.getProjection();
        float x=e.getX();
        float y=e.getY();
        GeoPoint point=(GeoPoint)projection.fromPixels(x,y);
        mainDrawer.addGeopointToMap("test","test_desc",point);

    }
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
}

问题在于,当我第一次长时间触摸时,一切都很好,添加了航点,但另一次它没有反应,它没有转到onTouch方法

我正在使用osmdroid地图。

1 个答案:

答案 0 :(得分:0)

如果你想检测地图上的长按,假设你正在使用v2 for Android,你不必实现你自己的手势检测器,而只需实现一个听众,如下所示:{{3} }

https://developers.google.com/maps/documentation/android/interactivity#map_clicklong_click_events