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

LatLngBounds not applied in OnMapReady #6769

Closed
tobrun opened this issue Oct 20, 2016 · 9 comments
Closed

LatLngBounds not applied in OnMapReady #6769

tobrun opened this issue Oct 20, 2016 · 9 comments
Assignees
Labels
Android Mapbox Maps SDK for Android bug

Comments

@tobrun
Copy link
Member

tobrun commented Oct 20, 2016

Current:
device-2016-10-20-124107

Desired result:
Map centers around 🇺🇸

@tobrun tobrun added bug Android Mapbox Maps SDK for Android labels Oct 20, 2016
@tobrun tobrun added this to the android-v4.2.0 milestone Oct 20, 2016
@tobrun
Copy link
Member Author

tobrun commented Oct 20, 2016

Noticing that viewportheight is 0, which means the MapView hasn't been measured yet.

@tobrun tobrun changed the title LatLngBounds not applied LatLngBounds not applied in OnMapReady Oct 20, 2016
@tobrun
Copy link
Member Author

tobrun commented Oct 20, 2016

Workaround for now is posting the update on the main thread:

        new Handler().post(new Runnable() {
            @Override
            public void run() {
                // Move camera to the bounds with added padding
                int padding = (int) getResources().getDimension(R.dimen.coordinatebounds_margin);
                mapboxMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));
            }
        });

device-2016-10-20-133547

@tobrun
Copy link
Member Author

tobrun commented Oct 20, 2016

Ideally this is fixed with a more robust camera system in #6532 (+ improvement over current implementation in #4796). One solution for now would be post every CameraUpdate we receive or selectively delay it for LatLngBoundsUpdate. The other option of forcing a measure on MapView didn't work since it triggers a render on core.

cc @mapbox/android

@tobrun tobrun self-assigned this Oct 25, 2016
@tobrun
Copy link
Member Author

tobrun commented Oct 25, 2016

Capturing from @ivovandongen to do a small test with the reverted OnMapReadyCallback (hooking into DID_FINISH_LOADING_MAP instead). This correctly animates the map:

ezgif com-video-to-gif 71

@gerhardol
Copy link

From the (duplicate) #6855
The workaround with post does not work for me (probably because the map is not visible at creation). I have to retry a couple of times (as described in the duplicate issue).

While I believe the final solution will work, the workaround may not work for everybody.

@tobrun
Copy link
Member Author

tobrun commented Nov 1, 2016

@gerhardol I would suggest using a addOnGlobalLayoutListener. When you receive this callback you are sure the view is measured and you can execute the LatLngBounds Camera update.

@gerhardol
Copy link

Thanks
The following works for me

                //Since MapBox 4.2.0-beta.3 moving the camera in onMapReady is not working if map is not visible
                //The workaround (so far) is to try move the camera at view updates as long as position is 0,0
                //https://github.com/mapbox/mapbox-gl-native/issues/6855#event-841575956 (and related issues)
                mapView.getViewTreeObserver().addOnGlobalLayoutListener(
                        new ViewTreeObserver.OnGlobalLayoutListener() {
                            @SuppressWarnings("deprecation")
                            // We check which build version we are using.
                            @Override
                            public void onGlobalLayout() {
                                if (map.getCameraPosition().target.getLatitude() != 0 ||
                                        map.getCameraPosition().target.getLongitude() != 0) {
                                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                                        mapView.getViewTreeObserver()
                                                .removeGlobalOnLayoutListener(this);
                                    } else {
                                        mapView.getViewTreeObserver()
                                                .removeOnGlobalLayoutListener(this);
                                    }

                                }
                                map.moveCamera(initialCameraPosition);
                            }
                        });

@tobrun
Copy link
Member Author

tobrun commented Nov 10, 2016

In #6919 I'm introducing the runnable around the camera methods for another issue (running an animation in onFinish). To completely resolve this issue we need to optimise the OnMapReadyCallback to take in account if style is loaded but also if the view is measured yet.

@tobrun
Copy link
Member Author

tobrun commented Dec 6, 2016

#6999 is the issue tracking the solution for this. Closing as a duplicate

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Android Mapbox Maps SDK for Android bug
Projects
None yet
Development

No branches or pull requests

2 participants