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] 4.2.0-beta.3, 4: moveCamera() is ignored if map is not displayed #6855

Closed

Description

@gerhardol

Android SDK 4.2.0-beta.3, beta.4
At least Android 6.0, 7.0

Since beta.3 (beta.1 definitely ok, beta.2 very likely OK) MapBox methods to move the camera requires that the map is displayed. Otherwise the requests are ignored.
If the activity is done in such way that the MapView is not viewed when the activity is first displayed, the request is ignored. In the application I work with, the interface is tabbed and the map is not default.
I have not found a callback that I can use when the map is displayed, so the mapBoxMap.moveCamera() is periodically attempted until position is no longer 0,0.

Workaround here:
https://github.com/gerhardol/runnerup/blob/mapbox-4.2-beta4-update/app/latest/java/org/runnerup/util/MapWrapper.java#L241

Note: The app has a separate build for Android versions before 4.0, the MapBox calls are in a Wrapper.
The calls are from a normal AppCompatActivity. (if you delay the moveCamera() call enough (with sleep) to be able to switch tabs, the camera moves).
https://github.com/gerhardol/runnerup/blob/mapbox-4.2-beta4-update/app/src/org/runnerup/view/DetailActivity.java#L148

I looked at the source, did not see any obvious.

If not fixed, there should be a better workaround (that is documented).

Steps to trigger behavior

Create MapBox view but do not display the map view.
Call mapBoxMap.moveCamera() in the onMapReady() callout.
(same for other camera position related calls).

You can use the master branch of RunnerUp to recreate too:

  • update mapbox from beta.2
  • record an activity with GPS (OK in emulator)
  • view the activity
  • select map tab

Expected behavior

Camera position to move

Actual behavior

Nothing

Activity

  1. changed the title [-]Android[/-] [+][Android] 4.2.0-beta.3, 4: moveCamera() is ignored if map is not displayed[/+] on Oct 29, 2016
  2. tobrun commented on Oct 31, 2016

    @tobrun
    Member

    @gerhardol thank you for reaching out, we recently changed the way the OnMapReadyCallback is invoked to align it correctly with a loaded style. The reason why moveCamera-LatLngBounds is not working now is that the MapView itself isn't measured yet and uses width = 0 when calculating the bounds. This issue is being tracked in #6769 and a workaround for this is to post you moveCamera(latlngbounds) to a runnable as shown here. Closing as a duplicate. Thank your for reaching out.

  3. added 2 commits that reference this issue on Nov 19, 2016
  4. gerhardol commented on Mar 23, 2017

    @gerhardol
    Author

    Still occurs in 5.0.1, so #6769, #6999 does not seem to be solved correctly.
    In my case, the mapview is hidden when the main view is opened.

  5. tobrun commented on Mar 24, 2017

    @tobrun
    Member

    Please use a mapfragment for this use-case

  6. mattijsf commented on Jun 13, 2017

    @mattijsf

    I also still experience the issue using 5.1.0-beta.3. Sometimes the camera is working and sometimes not. I'm moving the camera in onMapReady using

    mHandler.post(new Runnable() {
        @Override
        public void run() {
            float width = map.getWidth();
            Log.d(TAG, "Moving camera with width: " + width);
            map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), padding));
        }
    });
    

    I do notice that the logged width is sometimes 0 when onMapReady occurs.

    My workaround is to wrap .getMapAsync in a OnGlobalLayoutListener which ensures the view has dimensions before setting up the map. Not pretty at all.

  7. tobrun commented on Jun 13, 2017

    @tobrun
    Member

    @mattijsf what happensif you replace map.getWidth() with mapView.getMeasuredWidth()?

  8. mattijsf commented on Jun 13, 2017

    @mattijsf

    If I revert the OnGlobalLayoutListener workaround and log mapView.getMeasuredWidth() I get the following:
    Moving camera with mapWidth: 0.0, mapViewMeasuredWidth: 0.0

    Edit:
    I get two occurrences alternating randomly it seems:
    Moving camera with mapWidth: 1017.0, mapViewMeasuredWidth: 1017.0
    Moving camera with mapWidth: 0.0, mapViewMeasuredWidth: 0.0

    It is pretty clear camera updates are ignored when the width reports 0

  9. mattijsf commented on Jun 13, 2017

    @mattijsf

    It's fair to mention that I'm using my own View which loaded into a hierarchy managed by React Native. I do however call the required lifecycle methods similar to a Fragment based implementation. The rest of the MapView seems to work fine, only the camera is currently showing this strange behavior.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions