[Android] Incorrect Anchor when reusing MarkerViewOptions
I'm experiencing "moving" Markers because the Anchor of a MarkerView seems faulty when reusing once set MarkerViewOptions when the map orientation changes, e.g. while using the locationtracking/bearingtracking
Platform: Android 23 Mapbox SDK version: 4.1.0-SNAPSHOT
Steps to trigger behavior
- Set up MarkerViewOptions
- Add new MarkerView to MapView
- Remove it
- Do it in a loop
Expected behavior
The MarkerView (here they are the location pins) stays at a stable position, it works when i constantly set up new MarkerViewOptions for adding a MarkerView.

Actual behavior
In this example the anchor is completly wrong and it is moving. The clips are quite short but you can take the lake or street as reference. I assume the true anchor point does not really change and just sticks with the initial one when setting up new MarkerViewOptions().

So for example if you want to reuse MarkerViewOptions, maybe for multiple locations that only differ in their coordinates you would always have to set up new MarkerViewOptions. I haven't tested yet whether this also affects the visibility option that would usually be the better practice than constantly recreating MarkerViews.
@Schumi09 thank you for the detailed report.
- Is it correct that the view and marker model used for
Tower indicatoris the same as the actualTower marker? - If yes, could you validate proposed solution below?
By looking at current anchor implementation:
/**
* Specifies the anchor being set on a particular point point of the MarkerView.
* <p>
* The anchor point is specified in the continuous space [0.0, 1.0] x [0.0, 1.0], where (0, 0)
* is the top-left corner of the image, and (1, 1) is the bottom-right corner.
* </p>
*
* @param u u-coordinate of the anchor, as a ratio of the image width (in the range [0, 1])
* @param v v-coordinate of the anchor, as a ratio of the image height (in the range [0, 1])
*/
public void setAnchor(@FloatRange(from = 0.0, to = 1.0) float u, @FloatRange(from = 0.0, to = 1.0) float v) {
this.anchorU = u;
this.anchorV = v;
}
You can see the values are set but aren't updated right away. The only time these are update is in the MarkerViewManager.update() method. This is the place where we do the actual x,y offset calculation. Once set these aren't updated anymore since the update() method is called on each draw.
public void update() {
for (final MarkerView marker : markerViewMap.keySet()) {
final View convertView = markerViewMap.get(marker);
if (convertView != null) {
PointF point = mapboxMap.getProjection().toScreenLocation(marker.getPosition());
if (marker.getOffsetX() == -1) {
int x = (int) (marker.getAnchorU() * convertView.getMeasuredWidth());
int y = (int) (marker.getAnchorV() * convertView.getMeasuredHeight());
marker.setOffsetX(x);
marker.setOffsetY(y);
}
convertView.setX(point.x - marker.getOffsetX());
convertView.setY(point.y - marker.getOffsetY());
// animate visibility
if (marker.isVisible() && convertView.getVisibility() == View.GONE) {
convertView.animate().cancel();
convertView.setAlpha(0);
AnimatorUtils.alpha(convertView, 1);
}
}
}
}
I'm currently thinking of setting offsetX and offsetY to -1 when the setter of anchor is called. This will result in recalculating the related values.
@Schumi09 Thanks for the feedback. We have now proposed a fix for this via https://github.com/mapbox/mapbox-gl-native/pull/5459. This should be available for you test in the SNAPSHOT later today and in 4.1.0 next week. Please reopen the ticket if the problem persists.
convertView.getMeasuredWidth() Gets the value of 0