【问题标题】:MapView onTouchListener never calledMapView onTouchListener 从未调用过
【发布时间】:2017-05-12 17:42:23
【问题描述】:

我正在尝试获取在 MapView 中发生的触摸事件,但是从未调用过侦听器。

地图活动

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

为什么从不调用监听器?我做错了什么?

【问题讨论】:

标签: android google-maps android-mapview


【解决方案1】:

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

【讨论】:

  • Log.d("debug", "Listener") 甚至没有被调用。问题是没有调用监听器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-01
  • 2014-11-23
  • 2017-08-27
  • 2012-08-09
相关资源
最近更新 更多