mapbox-plugins-android
mapbox-plugins-android copied to clipboard
mapView.setOnTouchListener#onTouch is not invoked with Annotations
I set onTouchListener
for MapView
and the code inside the block as below was invoked each time.
mapView.setOnTouchListener { view, event -> ... }
However, after initializing LineManager
, View.OnTouchListener
has no effect. What is causing it and how can I fix it?
Platform: Android mapboxAnnotationsVersion = '0.5.0'
As a workaround until there is a fix, this ugly trick solves the issue:
- Add an empty view on top of MapView
- Set the OnTouchListener for that view.
LineManager is an implementation of AnnotationManager which uses a DraggableAnnotationController which also sets a touchListener on the mapView.
Unfortunately, you can only set one touchListener on the View
(it's a setOnTouchListener
, not an addOnTouchListener
)
The current conclusion is that you can't do both at the same time. One other workaround would be to add a callback to be called before androidGesturesManager.onTouchEvent(event); is triggered. Another one if you are using the androidGesturesManager would be to have is injected in like it's done in the second constructor currently.
Could you explain your use case?
Thanks, in my use case I have a Mapview
inside a RecyclerView
. Based on the touch coordinate point, I set the flag for parent.requestDisallowInterceptTouchEvent()
.
Thanks, in my use case I have a
Mapview
inside aRecyclerView
. Based on the touch coordinate point, I set the flag forparent.requestDisallowInterceptTouchEvent()
.
You can use your custom Mapview. This mapview extends mapview and override onTouchEvent() Like below in my case: public static class CustomMapView extends MapView {
public CustomMapView(@NonNull Context context) {
super(context);
}
public CustomMapView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public CustomMapView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomMapView(@NonNull Context context, @Nullable MapboxMapOptions options) {
super(context, options);
}
List<MapTouchListener> mapDownUpListeners = new ArrayList<>();
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (event.getAction() == KeyEvent.ACTION_DOWN) {
for (MapTouchListener listener : mapDownUpListeners) {
listener.onTouchDown();
}
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP) {
for (MapTouchListener listener : mapDownUpListeners) {
listener.onTouchUp();
}
return true;
}
return true;
}
}
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions.
Thanks, in my use case I have a
Mapview
inside aRecyclerView
. Based on the touch coordinate point, I set the flag forparent.requestDisallowInterceptTouchEvent()
.You can use your custom Mapview. This mapview extends mapview and override onTouchEvent() Like below in my case: public static class CustomMapView extends MapView {
public CustomMapView(@NonNull Context context) { super(context); } public CustomMapView(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public CustomMapView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public CustomMapView(@NonNull Context context, @Nullable MapboxMapOptions options) { super(context, options); } List<MapTouchListener> mapDownUpListeners = new ArrayList<>(); @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); if (event.getAction() == KeyEvent.ACTION_DOWN) { for (MapTouchListener listener : mapDownUpListeners) { listener.onTouchDown(); } return true; } else if (event.getAction() == KeyEvent.ACTION_UP) { for (MapTouchListener listener : mapDownUpListeners) { listener.onTouchUp(); } return true; } return true; } }
Can someone confirm this works ? Not working for me
Getting the same problem. I have my MapView inside a RecyclerView, trying to catch onToucheEvent callback but do not get anything so far. Huge problem as my MapView is not scrollable inside my RecyclerView. Does anyone has a fix for this ?
I just pushed a PR to fix this issue, see #1157. Hope this will help.