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.

Camera animations do not respect map padding #5034

Closed

Description

@nealsanche

Platform:Android
Mapbox SDK version:4.1.0-SNAPSHOT

Steps to trigger behavior

  1. set map padding
  2. perform an animateCamera to a particular latLngBounds:
           mMap.animateCamera(
                    CameraUpdateFactory.newLatLngBounds(latLngBounds, (int) (16 * getDensity())));

Expected behavior

Expect the camera animation to respect the padding.

Actual behavior

The camera animation occurs but with respect to the visible area of the map, not regarding padding.

While on the topic, I'd also expect the map's projection getVisibleBounds to also take into account the padding. This seems to be how google maps behave.

I ended up having to write:

    @NonNull
    private LatLngBounds getVisibleBoundsIncludingPadding() {
        // GetVisibleRegion doesn't take the padding into account, so we need to do our own
        // calculations
        Projection projection = mMap.getProjection();

        int viewportWidth = mBinding.map.getWidth();
        int viewportHeight = mBinding.map.getHeight();

        viewportHeight = viewportHeight - mMap.getPadding()[1];
        viewportHeight = viewportHeight - mMap.getPadding()[3];
        viewportWidth = viewportWidth - mMap.getPadding()[0];
        viewportWidth = viewportWidth - mMap.getPadding()[2];

        LatLngBounds.Builder builder = new LatLngBounds.Builder();

        LatLng topLeft = projection.fromScreenLocation(new PointF(0, 0));
        LatLng topRight = projection.fromScreenLocation(new PointF(viewportWidth, 0));
        LatLng bottomRight = projection.fromScreenLocation(new PointF(viewportWidth, viewportHeight));
        LatLng bottomLeft = projection.fromScreenLocation(new PointF(0, viewportHeight));

        builder.include(topLeft)
                .include(topRight)
                .include(bottomRight)
                .include(bottomLeft);

        return builder.build();
    }

to compensate for this difference while porting a project away from Google maps.

Activity

  1. added
    Android
    on May 13, 2016
  2. tobrun commented on May 17, 2016

    @tobrun
    Member

    @nealsanche thank you for reaching out and reporting both issues.

  3. tobrun commented on May 17, 2016

    @tobrun
    Member

    In LatLngBoundsActivity, I added some padding to the map:

    // Add map padding
    int mapPadding = (int) getResources().getDimension(R.dimen.fab_margin);
    mapboxMap.setPadding(mapPadding, mapPadding, mapPadding, mapPadding);    
    

    Without any fixes this results in the following screenshot where no extra padding was added:

    device-2016-05-17-094612

    After fixing this in the camera update method:

                // calculate correct padding
                int[] mapPadding = mapboxMap.getPadding();
                RectF latLngPadding = getPadding();
                RectF padding = new RectF(latLngPadding.left + mapPadding[0],
                        latLngPadding.top + mapPadding[1],
                        latLngPadding.right + mapPadding[2],
                        latLngPadding.bottom + mapPadding[3]);

    I'm seeing the padding being applied:

    device-2016-05-17-094719

  4. tobrun commented on May 17, 2016

    @tobrun
    Member

    I resolved the second issue by introducing two new methods in the content padding part of MapView:

        //
        // Content padding
        //
    
        int getContentPaddingLeft() {
            return mContentPaddingLeft;
        }
    
        int getContentPaddingTop() {
            return mContentPaddingTop;
        }
    
        int getContentPaddingRight() {
            return mContentPaddingRight;
        }
    
        int getContentPaddingBottom() {
            return mContentPaddingBottom;
        }
    
        int getContentWidth(){
            return getWidth() - mContentPaddingLeft - mContentPaddingRight;
        }
    
        int getContentHeight(){
            return getHeight() - mContentPaddingBottom - mContentPaddingTop;
        }
    

    The getContentWidth and getContentHeight are now being used over getHeight and getWidth.

     public VisibleRegion getVisibleRegion() {
            LatLngBounds.Builder builder = new LatLngBounds.Builder();
    
            int viewportWidth = mMapView.getContentWidth();
            int viewportHeight = mMapView.getContentHeight();
    
            LatLng topLeft = fromScreenLocation(new PointF(0, 0));
            LatLng topRight = fromScreenLocation(new PointF(viewportWidth, 0));
            LatLng bottomRight = fromScreenLocation(new PointF(viewportWidth, viewportHeight));
            LatLng bottomLeft = fromScreenLocation(new PointF(0, viewportHeight));
    
            builder.include(topLeft)
                    .include(topRight)
                    .include(bottomRight)
                    .include(bottomLeft);
    
            return new VisibleRegion(topLeft, topRight, bottomLeft, bottomRight, builder.build());
        }
  5. added 2 commits that reference this issue on May 17, 2016
  6. tobrun commented on May 17, 2016

    @tobrun
    Member

    @bleege @zugaldia @cammace
    How do you feel about including this in 4.1.0?

    This is a bug that makes migration from Google Maps more difficult since our API is not behaving the same way. 0e9f7ae & 9e1b82b fixes this behaviour to match the end developer expectations.

  7. nealsanche commented on May 17, 2016

    @nealsanche
    Author

    It did occur to me afterwards, that the getVisibleRegion() implementation may also need to account for the fact that the viewport won't always start at 0,0 on the screen, and must also take into account the top padding. Thanks for looking into this issue so quickly.

  8. added this to the android-v4.1.0 milestone on May 18, 2016
  9. tobrun commented on May 18, 2016

    @tobrun
    Member

    @nealsanche thanks for adding that remark. Without looking into it I'm currently thinking the x,y position is relative to the parent ViewGroup. In this case that would be MapView (extending FrameLayout) but I will definitely look into this before merging.

  10. tobrun commented on May 20, 2016

    @tobrun
    Member

    I'm going to test out the remarks made above and see if we need to include a fix for it.
    Afterwards I will rebase and merge #5051.

  11. tobrun commented on May 20, 2016

    @tobrun
    Member

    I'm doing some test with the MapPaddingActivitiy, added some markers in the padding area and see if getAnnotationsInBounds returns the correct markers. I'm currently not receiving the results I though I would get back. Not sure if this is an error in calculating the bounds or within LatLngBounds or something else. Will need to dig further to find out where this is coming from.

  12. self-assigned this
    on May 23, 2016
  13. tobrun commented on May 26, 2016

    @tobrun
    Member

    I have been debugging this and I started to notice that a map facing north doesn't have the issue mentioned above. I'm currently guessing the logic behind LatLngBounds works correctly when assumed the projection is rectangular. In the case I was looking into the projection is a parallelogram. If we also have tilt enabled this projection becomes a trapezoid. Since this is not related to the issue above. I'm going to merge the changes for padding and create a follow up ticket on this.

  14. tobrun commented on May 26, 2016

    @tobrun
    Member

    @nealsanche
    You were right in #5034 (comment). I ended up using following code:

            float left = mMapView.getContentPaddingLeft();
            float right = mMapView.getWidth() - mMapView.getContentPaddingRight();
            float top = mMapView.getContentPaddingTop();
            float bottom = mMapView.getHeight() - mMapView.getContentPaddingBottom();
    
            LatLng topLeft = fromScreenLocation(new PointF(left, top));
            LatLng topRight = fromScreenLocation(new PointF(right, top));
            LatLng bottomRight = fromScreenLocation(new PointF(right, bottom));
            LatLng bottomLeft = fromScreenLocation(new PointF(left, bottom));

    Thanks again for reaching out and reporting this issue!

  15. nealsanche commented on May 26, 2016

    @nealsanche
    Author

    You are welcome. Thanks for getting this in!

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