mapbox-gl-native icon indicating copy to clipboard operation
mapbox-gl-native copied to clipboard

OnCameraChangeListener

Open tobrun opened this issue 10 years ago • 21 comments

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.

tobrun avatar Jan 29 '16 10:01 tobrun

OnCameraChangeListener will be added in 4.0.0?

Trinea avatar Feb 01 '16 09:02 Trinea

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

Trinea avatar Feb 01 '16 09:02 Trinea

@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

tobrun avatar Feb 09 '16 08:02 tobrun

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.

tobrun avatar Feb 09 '16 09:02 tobrun

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();
                }
            }
        });
    }

tobrun avatar Feb 09 '16 10:02 tobrun

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.

tobrun avatar Feb 09 '16 10:02 tobrun

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

tobrun avatar Feb 09 '16 10:02 tobrun

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

tobrun avatar Feb 09 '16 10:02 tobrun

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.

tobrun avatar Feb 09 '16 10:02 tobrun

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

tobrun avatar Feb 09 '16 11:02 tobrun

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.

tobrun avatar Feb 09 '16 11:02 tobrun

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.

tobrun avatar Feb 09 '16 11:02 tobrun

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

Trinea avatar Feb 17 '16 10:02 Trinea

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.

mpuchala avatar Mar 14 '16 15:03 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.

mpuchala avatar Mar 15 '16 15:03 mpuchala

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

tobrun avatar Mar 15 '16 16:03 tobrun

Sure, thanks!

mpuchala avatar Mar 15 '16 16:03 mpuchala

I created these 2 follow up tickets:

  • #4327
  • #4326

tobrun avatar Mar 15 '16 16:03 tobrun

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.

programmerr47 avatar Apr 18 '16 16:04 programmerr47

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

1ec5 avatar May 24 '16 03:05 1ec5