Android 从未调用MapView onTouchListener
我试图获取MapView中发生的触摸事件,但是,从未调用侦听器 地图活动Android 从未调用MapView onTouchListener,android,google-maps,android-mapview,Android,Google Maps,Android Mapview,我试图获取MapView中发生的触摸事件,但是,从未调用侦听器 地图活动 public class MapActivity extends AppCompatActivity implements OnMapReadyCallback { private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey"; private MapView mapView; private GoogleMap map;
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;
}
}
为什么听众从来没有打过电话?我做错了什么?在
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;
}
}
public void onCreate(Bundle savedInstanceStates){
super.onCreate(savedInstanceStates);
setContentView(R.layout.map);
MapView MapView=(MapView)findViewById(R.id.view\u map);
mapOverlay myOverlay=新的mapOverlay();
List overlays=mMapView.getOverlays();
覆盖层。添加(myOverlay);
}
类mapOverlay扩展了com.google.android.maps.Overlay{
@凌驾
公共布尔onTouchEvent(MotionEvent事件,MapView MapView){
开关(event.getAction()){
案例(MotionEvent.ACTION\u DOWN):
Log.d(“调试”,“操作已停止”);
打破
案例(MotionEvent.ACTION\u MOVE):
Log.d(“调试”,“操作被移动”);
打破
案例(MotionEvent.ACTION\u UP):
Log.d(“调试”,“操作已启动”);
打破
违约:
打破
}
返回true;
}
}
在地图视图中注册一个覆盖,并将触摸事件
添加到该视图中
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;
}
}
public void onCreate(Bundle savedInstanceStates){
super.onCreate(savedInstanceStates);
setContentView(R.layout.map);
MapView MapView=(MapView)findViewById(R.id.view\u map);
mapOverlay myOverlay=新的mapOverlay();
List overlays=mMapView.getOverlays();
覆盖层。添加(myOverlay);
}
类mapOverlay扩展了com.google.android.maps.Overlay{
@凌驾
公共布尔onTouchEvent(MotionEvent事件,MapView MapView){
开关(event.getAction()){
案例(MotionEvent.ACTION\u DOWN):
Log.d(“调试”,“操作已停止”);
打破
案例(MotionEvent.ACTION\u MOVE):
Log.d(“调试”,“操作被移动”);
打破
案例(MotionEvent.ACTION\u UP):
Log.d(“调试”,“操作已启动”);
打破
违约:
打破
}
返回true;
}
}
MapView似乎无法与触控监听器配合使用。您可以使用“单击侦听器”,也可以使用“地图视图”似乎不适用于触摸侦听器。您可以使用Click Listener或Log.d(“调试”、“侦听器”)
甚至不会被调用。问题是侦听器没有被调用Log.d(“调试”,“侦听器”)
甚至没有被调用。问题是没有调用侦听器