Skip to content

Commit

Permalink
Merge pull request #718 from mfazekas/fix-marker-view2
Browse files Browse the repository at this point in the history
Fix MarkerView resize issue
  • Loading branch information
mfazekas authored Mar 12, 2020
2 parents 77f4247 + 550c300 commit 0cae0b9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<MarkerView> 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +19,6 @@ public class RCTMGLMarkerView extends AbstractMapFeature implements MarkerView.O

private View mChildView;


private MarkerViewManager mMarkerViewManager;

private MarkerView mMarkerView;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@
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;
import com.mapbox.rctmgl.R;
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;
Expand Down Expand Up @@ -111,7 +112,6 @@ public class RCTMGLMapView extends MapView implements OnMapReadyCallback, Mapbox
private List<Pair<Integer, ReadableArray>> mPreRenderMethods = new ArrayList<>();

private MapboxMap mMap;
private boolean mReflowDone = false;

private String mStyleURL;

Expand All @@ -136,10 +136,7 @@ public class RCTMGLMapView extends MapView implements OnMapReadyCallback, Mapbox

private HashSet<String> mHandledMapChangedEvents = null;

private final List<OnMapReadyCallback> onMapAndReflowReadyCallbackList = new ArrayList<>();


private MarkerViewManager makerViewManager = null;
private MarkerViewManager markerViewManager = null;
private ViewGroup mOffscreenAnnotationViewContainer = null;

private boolean mAnnotationClicked = false;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
}
}

Expand Down Expand Up @@ -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;
}

}

0 comments on commit 0cae0b9

Please sign in to comment.