Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

[Android] Incorrect Anchor when reusing MarkerViewOptions #5421

Closed

Description

@Schumi09

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

  1. Set up MarkerViewOptions
  2. Add new MarkerView to MapView
  3. Remove it
  4. 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.
stable_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().
unstable_markerview

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.

Activity

  1. added
    Android
    on Jun 21, 2016
  2. tobrun commented on Jun 23, 2016

    @tobrun
    Member

    @Schumi09 thank you for the detailed report.

    • Is it correct that the view and marker model used for Tower indicator is the same as the actual Tower 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.

  3. self-assigned this
    on Jun 23, 2016
  4. added this to the android-v4.1.0 milestone on Jun 23, 2016
  5. added a commit that references this issue on Jun 23, 2016
    6b1a753
  6. zugaldia commented on Jun 23, 2016

    @zugaldia
    Member

    @Schumi09 Thanks for the feedback. We have now proposed a fix for this via #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.

  7. shiyuankao commented on Jul 8, 2016

    @shiyuankao

    convertView.getMeasuredWidth() Gets the value of 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Android

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions