Skip to content
This repository has been 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
gerhardol opened this issue Oct 29, 2016 · 7 comments
Closed

Comments

@gerhardol
Copy link

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

@gerhardol gerhardol changed the title Android [Android] 4.2.0-beta.3, 4: moveCamera() is ignored if map is not displayed Oct 29, 2016
@tobrun
Copy link
Member

tobrun commented Oct 31, 2016

@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.

@tobrun tobrun closed this as completed Oct 31, 2016
gerhardol added a commit to gerhardol/runnerup that referenced this issue Nov 19, 2016
with beta.3 and later a workaround is needed to zoom to activity when opening.
The workaround (so far) is to try move the camera at view updates as long as position is 0,0
mapbox/mapbox-gl-native#6855 (comment) (and related issues)

beta.2 works fine (4.1.1 had marker issues) but beta.5 should be better
gerhardol added a commit to gerhardol/runnerup that referenced this issue Nov 20, 2016
with beta.3 and later a workaround is needed to zoom to activity when opening.
The workaround (so far) is to try move the camera at view updates as long as position is 0,0
mapbox/mapbox-gl-native#6855 (comment) (and related issues)

beta.2 works fine (4.1.1 had marker issues) but beta.5 should be better
@gerhardol
Copy link
Author

gerhardol commented Mar 23, 2017

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.

@tobrun
Copy link
Member

tobrun commented Mar 24, 2017

Please use a mapfragment for this use-case

@mattijsf
Copy link

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.

@tobrun
Copy link
Member

tobrun commented Jun 13, 2017

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

@mattijsf
Copy link

mattijsf commented Jun 13, 2017

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

@mattijsf
Copy link

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants