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.

LatLngBounds not applied in OnMapReady #6769

Closed

Description

@tobrun

Current:
device-2016-10-20-124107

Desired result:
Map centers around 🇺🇸

Activity

  1. added
    Android
    on Oct 20, 2016
  2. added this to the android-v4.2.0 milestone on Oct 20, 2016
  3. tobrun commented on Oct 20, 2016

    @tobrun
    MemberAuthor

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

  4. changed the title [-]LatLngBounds not applied[/-] [+]LatLngBounds not applied in OnMapReady[/+] on Oct 20, 2016
  5. tobrun commented on Oct 20, 2016

    @tobrun
    MemberAuthor

    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

  6. tobrun commented on Oct 20, 2016

    @tobrun
    MemberAuthor

    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

  7. self-assigned this
    on Oct 25, 2016
  8. tobrun commented on Oct 25, 2016

    @tobrun
    MemberAuthor

    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

  9. removed this from the android-v4.2.0 milestone on Oct 25, 2016
  10. gerhardol commented on Nov 1, 2016

    @gerhardol

    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.

  11. tobrun commented on Nov 1, 2016

    @tobrun
    MemberAuthor

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

  12. gerhardol commented on Nov 1, 2016

    @gerhardol

    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);
                                }
                            });
    
  13. added this to the android-v5.0.0 milestone on Nov 3, 2016
  14. tobrun commented on Nov 10, 2016

    @tobrun
    MemberAuthor

    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.

  15. tobrun commented on Dec 6, 2016

    @tobrun
    MemberAuthor

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

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

Metadata

Metadata

Assignees

Labels

Androidbug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions