从未调用过MapView onTouchListener

时间:2017-05-12 17:42:23

标签: android google-maps android-mapview

我试图控制在MapView中发生的触摸事件,但是,从不调用侦听器。

MapActivity

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {

    private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey";

    private MapView mapView;
    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        // *** IMPORTANT ***
        // MapView requires that the Bundle you pass contain _ONLY_ MapView SDK
        // objects or sub-Bundles.
        Bundle mapViewBundle = null;
        if (savedInstanceState != null) {
            mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY);
        }
        mapView = (MapView) findViewById(R.id.view_map);
        mapView.onCreate(mapViewBundle);

        mapView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {

                Log.d("debug", "Listener");
                int action = MotionEventCompat.getActionMasked(event);

                switch (action) {

                    case (MotionEvent.ACTION_DOWN) :
                        Log.d("debug","Action was DOWN");
                        break;
                    case (MotionEvent.ACTION_MOVE) :
                        Log.d("debug","Action was MOVE");
                        break;
                    case (MotionEvent.ACTION_UP) :
                        Log.d("debug","Action was UP");
                        break;
                    default:
                        break;
                }
                return true;
            }
        });

        mapView.getMapAsync(this);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        Bundle mapViewBundle = outState.getBundle(MAPVIEW_BUNDLE_KEY);
        if (mapViewBundle == null) {
            mapViewBundle = new Bundle();
            outState.putBundle(MAPVIEW_BUNDLE_KEY, mapViewBundle);
        }

        mapView.onSaveInstanceState(mapViewBundle);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    protected void onPause() {
        mapView.onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mapView.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {

        map = googleMap;
    }
}

为什么听众从不打电话?我做错了什么?

1 个答案:

答案 0 :(得分:0)

将叠加层注册到MapView并将TouchEvent添加到

 public void onCreate(Bundle savedInstanceStates){       
    super.onCreate(savedInstanceStates);
    setContentView(R.layout.map);

    MapView mapview=(MapView)findViewById(R.id.view_map);

    mapOverlay myOverlay = new mapOverlay();
    List<Overlay> overlays = mMapView.getOverlays();        
    overlays.add(myOverlay);
}        



class mapOverlay extends com.google.android.maps.Overlay{
    @Override

    public boolean onTouchEvent(MotionEvent event, MapView mapview){

        switch (event.getAction()) {

                    case (MotionEvent.ACTION_DOWN) :
                        Log.d("debug","Action was DOWN");
                        break;
                    case (MotionEvent.ACTION_MOVE) :
                        Log.d("debug","Action was MOVE");
                        break;
                    case (MotionEvent.ACTION_UP) :
                        Log.d("debug","Action was UP");
                        break;
                    default:
                        break;
                }
                return true;
    }
}