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.

OnCameraChangeListener #3745

Closed
Closed

Description

@tobrun

Callback interface for when the camera changes position.

GoogleMap equivalent:

Called after the camera position has changed. During an animation, this listener may not be notified of intermediate camera positions. It is always called for the final position in the animation.

Activity

  1. added
    Android
    on Jan 29, 2016
  2. added this to the android-v4.0.0 milestone on Jan 29, 2016
  3. Trinea commented on Feb 1, 2016

    @Trinea

    OnCameraChangeListener will be added in 4.0.0?

  4. Trinea commented on Feb 1, 2016

    @Trinea

    Is there any way to achieve functional equals to OnCameraChangeListener on version 3.1.0? thank you.

  5. tobrun commented on Feb 9, 2016

    @tobrun
    MemberAuthor

    @Trinea:

    OnCameraChangeListener will be added in 4.0.0?

    I currently can't give a ETA for this, mainly because I first need to first need to investigate if we actually are going to add this (check if there is no issue for performance and if we are willing to work around some of these issues).

    Is there any way to achieve this on version 3.1.0?

    Yes you could, first you need to listen to Map change events:

    private class MapChangeCameraPositionListener implements MapView.OnMapChangedListener {
            @Override
            public void onMapChanged(@MapView.MapChange int change) {
                if (!mInvalidCameraPosition && (change == MapView.REGION_DID_CHANGE
                        || change == MapView.REGION_DID_CHANGE_ANIMATED
                        || change == MapView.REGION_WILL_CHANGE
                        || change == MapView.REGION_WILL_CHANGE_ANIMATED)) {
                    mInvalidCameraPosition = true;
             }
         }
    }

    Afterwards you will be able to

    • Reuse the current CameraPosition from the Camera API
    • Recalculate CameraPosition if mInvalidCameraPosition is true
  6. tobrun commented on Feb 9, 2016

    @tobrun
    MemberAuthor

    I started investigating what needs to be done to actually have this in place:

    • I identified 2 items:
      • we need a callback system that is invoked when a animation using the Camera API has finished.
      • we need call the listener is camera position changed through gestures
        • updating on every change will not be performant.
  7. tobrun commented on Feb 9, 2016

    @tobrun
    MemberAuthor

    The callback invoked if camera API animation has finished will look like:

     @UiThread
     public final void animateCamera(CameraUpdate update, int durationMs, final MapboxMap.CancelableCallback callback) {
            mCameraPosition = update.getCameraPosition(this);
            mMapView.flyTo(mCameraPosition.bearing, mCameraPosition.target, getDurationNano(durationMs), mCameraPosition.tilt, mCameraPosition.zoom, new CancelableCallback() {
                @Override
                public void onCancel() {
                    if (callback != null) {
                        callback.onCancel();
                    }
                }
    
                @Override
                public void onFinish() {
                    if (mOnCameraChangeListener != null) {
                        mOnCameraChangeListener.onCameraChange(mCameraPosition);
                    }
    
                    if (callback != null) {
                        callback.onFinish();
                    }
                }
            });
        }
  8. tobrun commented on Feb 9, 2016

    @tobrun
    MemberAuthor

    The callback invoked if camera position changed through gestures is not performant because we are have a flood of events when doing gestures (best example is panning). I'm thinking about implementing an idle timeout. Which means I will listen to MapChange events and will only invoke the callback if a certain timeout has passed and in that case the timeout will be reset.

  9. tobrun commented on Feb 9, 2016

    @tobrun
    MemberAuthor

    I created these 2 clips to show the difference in performance by adding a CameraChangeListener:

    This has given me enough evidence to optimise my remark above

  10. tobrun commented on Feb 9, 2016

    @tobrun
    MemberAuthor

    To determine a good idle time, we first need to know how many invokes are occurring for each second:

    02-09 11:32:38.000 5081-5081/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=15.444507542844377, latitude=35.439443032337685, altitude=0.0], Zoom:5.0, Bearing:-0.0, Tilt:0.0
    ...
    110 other OnCameraChange events within the second
    ...
    02-09 11:32:39.000 5081-5081/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=15.451831725430223, latitude=35.59444138082618, altitude=0.0], Zoom:5.0, Bearing:-0.0, Tilt:0.0
    

    That are 112 callbacks occurring in one second! which is way too much

  11. tobrun commented on Feb 9, 2016

    @tobrun
    MemberAuthor

    View the the result after rate limiting the updates to 250 ms here

    The logs for 250ms:

    02-09 11:53:05.782 6613-6613/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=13.095548162950479, latitude=34.90384096611634, altitude=0.0], Zoom:4.8878264, Bearing:-0.0, Tilt:0.0
    02-09 11:53:06.036 6613-6613/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=15.866284686229164, latitude=35.163123436805506, altitude=0.0], Zoom:4.8878264, Bearing:-0.0, Tilt:0.0
    02-09 11:53:06.290 6613-6613/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=12.858056400227182, latitude=34.86487770058235, altitude=0.0], Zoom:4.8878264, Bearing:-0.0, Tilt:0.0
    02-09 11:53:06.545 6613-6613/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=12.501818800025745, latitude=35.14370580034226, altitude=0.0], Zoom:4.8878264, Bearing:-0.0, Tilt:0.0
    02-09 11:53:06.798 6613-6613/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=15.304220960605845, latitude=35.11780840789882, altitude=0.0], Zoom:4.8878264, Bearing:-0.0, Tilt:0.0
    02-09 11:53:07.171 6613-6613/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=15.351719302958456, latitude=35.11780840789882, altitude=0.0], Zoom:4.8878264, Bearing:-0.0, Tilt:0.0
    

    You can see we are having max 4 updates / sec.

  12. tobrun commented on Feb 9, 2016

    @tobrun
    MemberAuthor

    View the result after rate limiting the updates to 100ms here.

    The logs for 100ms:

    02-09 11:58:05.958 15570-15570/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=10.057959502714567, latitude=34.83925559295183, altitude=0.0], Zoom:4.891043, Bearing:-0.0, Tilt:0.0
    02-09 11:58:06.060 15570-15570/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=12.10373799956696, latitude=35.35625570937396, altitude=0.0], Zoom:4.891043, Bearing:-0.0, Tilt:0.0
    02-09 11:58:06.162 15570-15570/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=10.96631674224227, latitude=34.80683436334074, altitude=0.0], Zoom:4.891043, Bearing:-0.0, Tilt:0.0
    02-09 11:58:06.264 15570-15570/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=9.670920305149366, latitude=34.51446980621013, altitude=0.0], Zoom:4.891043, Bearing:-0.0, Tilt:0.0
    02-09 11:58:06.365 15570-15570/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=9.915781834425303, latitude=34.60553817305581, altitude=0.0], Zoom:4.891043, Bearing:-0.0, Tilt:0.0
    02-09 11:58:06.467 15570-15570/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=12.048446689966397, latitude=35.10462770788207, altitude=0.0], Zoom:4.891043, Bearing:-0.0, Tilt:0.0
    02-09 11:58:06.570 15570-15570/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=11.187481974990561, latitude=34.82628862695539, altitude=0.0], Zoom:4.891043, Bearing:-0.0, Tilt:0.0
    02-09 11:58:06.670 15570-15570/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=9.536641380241889, latitude=34.677021913056244, altitude=0.0], Zoom:4.891043, Bearing:-0.0, Tilt:0.0
    02-09 11:58:06.772 15570-15570/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=9.836794223354588, latitude=34.78088818821665, altitude=0.0], Zoom:4.891043, Bearing:-0.0, Tilt:0.0
    02-09 11:58:06.873 15570-15570/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=11.764091368219994, latitude=35.12401140100763, altitude=0.0], Zoom:4.891043, Bearing:-0.0, Tilt:0.0
    02-09 11:58:06.975 15570-15570/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=10.792544050994309, latitude=34.97528529302905, altitude=0.0], Zoom:4.891043, Bearing:-0.0, Tilt:0.0
    02-09 11:58:07.077 15570-15570/com.mapbox.mapboxsdk.testapp V/TAG: OnCameraChange : Target: LatLng [longitude=9.481350111602467, latitude=34.78737549317813, altitude=0.0], Zoom:4.891043, Bearing:-0.0, Tilt:0.0
    

    You can see we are having max 10 updates / sec

  13. tobrun commented on Feb 9, 2016

    @tobrun
    MemberAuthor

    There is visual not that much difference between 100/250ms, except when having both side-by-side:

    • Left is 250ms / Right is 100ms:

    screen shot 2016-02-09 at 11 59 00

    There is a light increase for 100 ms.

  14. tobrun commented on Feb 9, 2016

    @tobrun
    MemberAuthor

    I'm thinking about using 400ms, this will result in 2-3 updates / sec.
    We can revisit this later and tweak the configuration if needed.

  15. 1 remaining item

  16. added a commit that references this issue on Feb 9, 2016
  17. Trinea commented on Feb 17, 2016

    @Trinea

    @tobrun Thank you very much, I will try later.

  18. mpuchala commented on Mar 14, 2016

    @mpuchala

    Hi guys,

    I'm a bit confused about this listener. I'm setting new position (of course just once):

    mMapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));

    That works fine but the problem is with returned value, this is my basic code:

    private void onCameraChangeListener(CameraPosition cameraPosition) {
            Timber.e("cameraPosition.zoom: " + cameraPosition.zoom + " mMapboxMap.getCameraPosition().zoom: " + mMapboxMap.getCameraPosition().zoom);
        }
    

    and here is log:

    03-14 16:25:29.785 MapObserver: cameraPosition.zoom: 13.791596412658691 mMapboxMap.getCameraPosition().zoom: 13.791596412658691
    03-14 16:25:29.801 MapObserver: cameraPosition.zoom: 14.0 mMapboxMap.getCameraPosition().zoom: 14.0
    03-14 16:25:29.802 MapObserver: cameraPosition.zoom: 13.791596412658691 mMapboxMap.getCameraPosition().zoom: 14.0
    

    Getting new value from global MapboxMap object seems to be a strange approach, can you clarify why it works in that way? All in all after moving camera listener parameter has old value and I get it 3 times...

    When I change map position using gestures, it works fine and values are the same.

    Thank's in advance for any suggestions.

  19. mpuchala commented on Mar 15, 2016

    @mpuchala

    Another thing is getting notification after double tap (zoom-in gesture). I don't get new value of zoom - IMO callback isn't notified after it's finished. I came across MapView class:

            // Called for double taps
            @Override
            public boolean onDoubleTapEvent(MotionEvent e) {
                if (mDestroyed || !mMapboxMap.getUiSettings().isZoomGesturesEnabled()) {
                    return false;
                }
    
                switch (e.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        if (mQuickZoom) {
                            // insert here?
                            mQuickZoom = false;
                            break;
                        }
    
                        // Single finger double tap
                        if (mMapboxMap.getTrackingSettings().isLocationTrackingDisabled()) {
                            // Zoom in on gesture
                            zoom(true, e.getX(), e.getY());
                        } else {
                            // Zoom in on user location view
                            PointF centerPoint = mUserLocationView.getMarkerScreenPoint();
                            zoom(true, centerPoint.x, centerPoint.y);
                        }
                        break;
                }
    
                trackGestureEvent(MapboxEvent.GESTURE_DOUBLETAP, e.getX(), e.getY());
    
                return true;
            }
    

    Of course it's only my guess, but isn't a problem with tracking this gesture or maybe callback should be notified in other way?

    @tobrun I'd be grateful for any clarification, despite those two things (which blocks my in some situations) it works like a charm.

  20. tobrun commented on Mar 15, 2016

    @tobrun
    MemberAuthor

    @mpuchala don't hesitate to open new tickets on the issue tracker,
    sometimes these closed tickets are getting unnoticed.
    I'm going to create follow up tickets for this.

  21. mpuchala commented on Mar 15, 2016

    @mpuchala

    Sure, thanks!

  22. tobrun commented on Mar 15, 2016

    @tobrun
    MemberAuthor
  23. programmerr47 commented on Apr 18, 2016

    @programmerr47

    Hello. Sorry for inconvenience but could you please help me with my porblem.
    I'm writing android app with help of Android Studio. I'm using gradle and targetSdk 23. Also I'm using mapbox sdk 4.0.0-beta.2.

    In my fragment I initialize mapbox maps something like this:

        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            ...
            mapView.onCreate(null);
            setupMapView(getActivity());
            ...
        }
    
        private void setupMapView(Context context) {
            ...
            mapView.getMapAsync(this);
            ...
        }
    
        @Override
        public void onMapReady(@NonNull MapboxMap mapboxMap) {
            this.mapboxMap = mapboxMap;
            ...
            this.mapboxMap.setOnCameraChangeListener(this);
            ...
        }
    
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {
            Log.v("TestLog", "OnCameraChange: " + cameraPosition);
        }

    When I run my project and try to use scroll or fling gestures on the map (without stopping it), I see in logs that onCameraChange calls every 400 ms (average). But for me expected result that it called only one time: when camera will be stopped.

    Maybe described situation is correct, but as I understood MapBox's OnCameraChangeListener is analog of GoogleMaps's OnCameraChangeListener and last works in the way that I described (onCameraChange calls only when camera will be in "idle" state). At least in mapbox documentation said same thing as in google maps documentation:

    Called after the camera position has changed. During an animation, this listener may not be notified of intermediate camera positions. It is always called for the final position in the animation.

    I came to this problem, because earlier I used google maps and used onCameraChange callback as indicator that map is in idle state (because I hide controls when work with map and want to show them, when camera is idle and I don't touch to map), and now I need to migrate to mapbox (because you have more and more better detalization of maps for some countries, like Russia), and I stunned with this problem.

    So if it is not hard to you, could you please help me? Is calling onCameraChange during camera animation a bug or not? If not, does mapbox provide callbacks to know when camera is in the idle state?

    Thank you.

    BTW:
    I'm testing on Nexus 5, v.6.0.1. And the previous example worked on Google maps.

  24. 1ec5 commented on May 24, 2016

    @1ec5
    Contributor

    @programmerr47, this issue report tracked feature work that was completed long ago, so none of the developers who can help you out are paying attention to this page. If you still have this question, please open a new issue so we can prioritize it correctly. Thank you for understanding.

  25. programmerr47 commented on May 24, 2016

    @programmerr47

    @1ec5, thank you for your attention. I fully agree with your, so I've created an issue about month ago.
    Here it is: #4746
    I will glad if you will have some feedback about this problem. Thank you.

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