From 550c30045c83f5fab503d397020294a6e22ebc3d Mon Sep 17 00:00:00 2001 From: Miklos Fazekas Date: Thu, 12 Mar 2020 10:12:33 +0100 Subject: [PATCH] Fix MarkerView resize issue - attempt 2 --- .../components/annotation/MarkerView.java | 23 ++++++++++ .../annotation/MarkerViewManager.java | 45 +++++++++++++++++++ .../annotation/RCTMGLMarkerView.java | 8 +--- .../components/mapview/RCTMGLMapView.java | 44 ++++++------------ 4 files changed, 83 insertions(+), 37 deletions(-) create mode 100644 android/rctmgl/src/main/java/com/mapbox/rctmgl/components/annotation/MarkerView.java create mode 100644 android/rctmgl/src/main/java/com/mapbox/rctmgl/components/annotation/MarkerViewManager.java diff --git a/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/annotation/MarkerView.java b/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/annotation/MarkerView.java new file mode 100644 index 000000000..7a42211dd --- /dev/null +++ b/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/annotation/MarkerView.java @@ -0,0 +1,23 @@ +package com.mapbox.rctmgl.components.annotation; + +import android.view.View; + +import androidx.annotation.NonNull; + +import com.mapbox.mapboxsdk.geometry.LatLng; + +/** + * Subclass of MarkerView so we MarkerViewManager can implement remove/restoreViews + */ +public class MarkerView extends com.mapbox.mapboxsdk.plugins.markerview.MarkerView { + View view; + + public MarkerView(@NonNull LatLng latLng, @NonNull View view) { + super(latLng, view); + this.view = view; + } + + public View getView() { + return this.view; + } +} diff --git a/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/annotation/MarkerViewManager.java b/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/annotation/MarkerViewManager.java new file mode 100644 index 000000000..900083a0e --- /dev/null +++ b/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/annotation/MarkerViewManager.java @@ -0,0 +1,45 @@ +package com.mapbox.rctmgl.components.annotation; + +import androidx.annotation.NonNull; + +import com.mapbox.mapboxsdk.maps.MapView; +import com.mapbox.mapboxsdk.maps.MapboxMap; + +import java.util.ArrayList; +import java.util.List; + +/** + * Subclass of MarkerViewManager implementing removeViews and restoreViews + */ +public class MarkerViewManager extends com.mapbox.mapboxsdk.plugins.markerview.MarkerViewManager { + private final List markers = new ArrayList<>(); + private MapView mapView; + + public MarkerViewManager(MapView mapView, MapboxMap mapboxMap) { + super(mapView, mapboxMap); + this.mapView = mapView; + // this.mapboxMap = mapboxMap; + } + + public void addMarker(@NonNull MarkerView markerView) { + super.addMarker(markerView); + markers.add(markerView); + } + + public void removeMarker(@NonNull MarkerView markerView) { + super.removeMarker(markerView); + markers.remove(markerView); + } + + public void removeViews() { + for (MarkerView marker: markers) { + mapView.removeView(marker.getView()); + } + } + + public void restoreViews() { + for (MarkerView marker: markers) { + mapView.addView(marker.getView()); + } + } +} \ No newline at end of file diff --git a/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/annotation/RCTMGLMarkerView.java b/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/annotation/RCTMGLMarkerView.java index 5517aab25..74c4a53a4 100644 --- a/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/annotation/RCTMGLMarkerView.java +++ b/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/annotation/RCTMGLMarkerView.java @@ -9,8 +9,6 @@ import com.mapbox.geojson.Point; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; -import com.mapbox.mapboxsdk.plugins.markerview.MarkerView; -import com.mapbox.mapboxsdk.plugins.markerview.MarkerViewManager; import com.mapbox.rctmgl.components.AbstractMapFeature; import com.mapbox.rctmgl.components.mapview.RCTMGLMapView; import com.mapbox.rctmgl.utils.GeoJSONUtils; @@ -21,7 +19,6 @@ public class RCTMGLMarkerView extends AbstractMapFeature implements MarkerView.O private View mChildView; - private MarkerViewManager mMarkerViewManager; private MarkerView mMarkerView; @@ -65,12 +62,11 @@ public void addToMap(RCTMGLMapView mapView) { final RCTMGLMarkerView rctmglMarkerView = this; - mMapView.getMapWithReflowAsync( + mMapView.getMapAsync( new OnMapReadyCallback() { @Override public void onMapReady(@NonNull MapboxMap mapboxMap) { - MarkerViewManager markerViewManager = new MarkerViewManager(mMapView, mapboxMap); - mMarkerViewManager = mMapView.getMakerViewManager(mapboxMap); + mMarkerViewManager = mMapView.getMarkerViewManager(mapboxMap); if (mChildView != null) { mMarkerView = new MarkerView(GeoJSONUtils.toLatLng(mCoordinate), mChildView); diff --git a/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/mapview/RCTMGLMapView.java b/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/mapview/RCTMGLMapView.java index 10fc38d1d..64db47a4e 100644 --- a/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/mapview/RCTMGLMapView.java +++ b/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/mapview/RCTMGLMapView.java @@ -45,7 +45,6 @@ import com.mapbox.mapboxsdk.plugins.annotation.OnSymbolDragListener; import com.mapbox.mapboxsdk.plugins.annotation.Symbol; import com.mapbox.mapboxsdk.plugins.annotation.SymbolManager; -import com.mapbox.mapboxsdk.plugins.markerview.MarkerViewManager; import com.mapbox.mapboxsdk.style.expressions.Expression; import com.mapbox.mapboxsdk.style.layers.Layer; import com.mapbox.mapboxsdk.style.layers.Property; @@ -53,6 +52,8 @@ import com.mapbox.rctmgl.components.AbstractMapFeature; import com.mapbox.rctmgl.components.annotation.RCTMGLPointAnnotation; import com.mapbox.rctmgl.components.annotation.RCTMGLMarkerView; +import com.mapbox.rctmgl.components.annotation.MarkerView; +import com.mapbox.rctmgl.components.annotation.MarkerViewManager; import com.mapbox.rctmgl.components.camera.RCTMGLCamera; import com.mapbox.rctmgl.components.images.RCTMGLImages; import com.mapbox.rctmgl.components.mapview.helpers.CameraChangeTracker; @@ -111,7 +112,6 @@ public class RCTMGLMapView extends MapView implements OnMapReadyCallback, Mapbox private List> mPreRenderMethods = new ArrayList<>(); private MapboxMap mMap; - private boolean mReflowDone = false; private String mStyleURL; @@ -136,10 +136,7 @@ public class RCTMGLMapView extends MapView implements OnMapReadyCallback, Mapbox private HashSet mHandledMapChangedEvents = null; - private final List onMapAndReflowReadyCallbackList = new ArrayList<>(); - - - private MarkerViewManager makerViewManager = null; + private MarkerViewManager markerViewManager = null; private ViewGroup mOffscreenAnnotationViewContainer = null; private boolean mAnnotationClicked = false; @@ -472,31 +469,10 @@ public void run() { measure(View.MeasureSpec.makeMeasureSpec(getMeasuredWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(getMeasuredHeight(), View.MeasureSpec.EXACTLY)); layout(getLeft(), getTop(), getRight(), getBottom()); - mReflowDone = true; - - for (OnMapReadyCallback callback : onMapAndReflowReadyCallbackList) { - callback.onMapReady(mMap); - } - onMapAndReflowReadyCallbackList.clear(); } }); } - /** - * Sets a callback object which will be triggered when the {@link MapboxMap} instance is done with reflow - * - * @param callback The callback object that will be triggered when the map is ready and reflow finished - */ - @UiThread - public void getMapWithReflowAsync(final @NonNull OnMapReadyCallback callback) { - if (mMap == null || !mReflowDone) { - // Add callback to the list only if the style hasn't loaded, or the drawing surface isn't ready - onMapAndReflowReadyCallbackList.add(callback); - } else { - callback.onMapReady(mMap); - } - } - public void createSymbolManager(Style style) { symbolManager = new SymbolManager(this, mMap, style); symbolManager.setIconAllowOverlap(true); @@ -562,7 +538,13 @@ public boolean onTouchEvent(MotionEvent ev) { @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { if (!mPaused) { + if (markerViewManager != null) { + markerViewManager.removeViews(); + } super.onLayout(changed, left, top, right, bottom); + if (markerViewManager != null) { + markerViewManager.restoreViews(); + } } } @@ -1319,14 +1301,14 @@ public ViewGroup offscreenAnnotationViewContainer() { return mOffscreenAnnotationViewContainer; } - public MarkerViewManager getMakerViewManager(MapboxMap map) { - if (makerViewManager == null) { + public MarkerViewManager getMarkerViewManager(MapboxMap map) { + if (markerViewManager == null) { if (map == null) { throw new Error("makerViewManager should be called one the map has loaded"); } - makerViewManager = new MarkerViewManager(this, map); + markerViewManager = new MarkerViewManager(this, map); } - return makerViewManager; + return markerViewManager; } }