About the camera change event of Google Maps Android API

The Google Maps library for Android has an event GoogleMap.OnCameraChangeListener that occurs when the camera is changed (= change to position, rotation, tilt).

However, with the August 2016 release, `ʻOnCameraChangeListener`` has been deprecated and four new events have been added instead.

This release introduces a set of new camera change listeners for camera motion start, ongoing, and end events. You can also see why the camera is moving, whether it's caused by user gestures, built-in API animations or developer-controlled movements. Below is a summary of the new listeners. For details, see the guide to camera change events. (Issue 4636)

  • The onCameraMoveStarted() callback of the OnCameraMoveStartedListener is invoked when the camera starts moving. The callback method receives a reason for the camera motion.
  • The onCameraMove() callback of the OnCameraMoveListener is invoked multiple times while the camera is moving or the user is interacting with the touch screen.
  • The OnCameraIdle() callback of the OnCameraIdleListener is invoked when the camera stops moving and the user has stopped interacting with the map.
  • The OnCameraMoveCanceled() callback of the OnCameraMoveCanceledListener is invoked when the current camera movement has been interrupted.

onCameraMoveStarted-When the camera starts moving

Occurs once immediately after the camera starts moving. The same applies to moving the camera by methods such as ʻupdateCamera`` and ʻanimateCamera``, and by gestures such as dragging and pinching.

onCameraMoveIdle-When the camera has finished moving

Occurs once immediately after the camera movement is complete. This means that whenever any view change occurs, ʻonCameraMoveIdle`` is always called once at the end. The ʻonCameraMoveIdle is still called when the animation is cancelled. It is similar in position to the deprecated `ʻonCameraChange. By the way, after ʻonCameraChange`` is called, ʻonCameraMoveIdle`` is called.

If ʻonCameraMoveStarted`` and ʻonCameraMoveIdle are always paired, there are ** cases where they are not **. This is the case when the map is moved by dragging the map while the map is being moved by `ʻanimateCamera. This will be discussed later.

onCameraMove-when the camera moves

Occurs when the camera moves. For direct camera position movements with moveCamera, this event ** may or may not occur **. Animated moves with `ʻanimateCamera`` raise this event during the animation. It also occurs with gesture operations such as dragging and pinching.

onCameraMoveCanceled-When the animation is canceled

Occurs when an animated camera move, such as `ʻanimateCamera``, is canceled by some operation. "Some operation" is

is.

[Bonus] Callback of `ʻanimateCamera``

`ʻanimateCamera`` can specify a callback that can receive the completion / cancellation of the animation.

public final void animateCamera (
    CameraUpdate update, 
    GoogleMap.CancelableCallback callback)

Event occurrence for each case

I made a sample application and tried to find out what kind of event would occur in the actual operation.

Execution of updateCamera method

  1. onCameraMoveStarted
  2. onCameraMove ← May not occur
  3. ~~onCameraChange~~
  4. onCameraIdle

** It seems that ʻonCameraMove`` may or may not occur **, I was not sure about the condition (it seems that ʻonCameraMove does not occur because the position changes. ). Since `ʻonCameraChange is deprecated, it has a strikethrough.

Execution of animateCamera method (without interruption)

  1. onCameraMoveStarted
  2. onCameraMove
  3. onCameraMove
  4. ・ ・ ・
  5. animateCamera_onFinish
  6. ~~onCameraChange~~
  7. onCameraIdle

```onCameraMove is called multiple times during the move. The number of times depends on the speed of the animation. When the move is complete, the `ʻanimateCamera method callback will be notified ofʻonFinish`` and thenʻonCameraIdle`` will be called.

Execution of animateCamera method → Execution of stopAnimation

  1. onCameraMoveStarted
  2. onCameraMove
  3. ・ ・ ・
  4. onCameraMove
  5. Call stopAnimation
  6. onCameraMoveCanceled
  7. animateCamera_onCancel
  8. ~~onCameraChange~~
  9. onCameraIdle

If you call stopAnimation during animation, the camera will stop moving and you will be notified of the stop in the order of ʻonCameraMoveCanceled`` → ʻanimateCamera_onCancel. After that, `ʻonCameraIdle is called just as it was when it was completed.

Execution of animateCamera method → Drag while moving to move the map

  1. onCameraMoveStarted ← Start moving by animateCamera
  2. onCameraMove
  3. ・ ・ ・
  4. onCameraMove
  5. Drag to move the map
  6. onCameraMoveCanceled
  7. onCameraMoveStarted ← Start moving by dragging
  8. animateCamera_onCancel
  9. onCameraMove
  10. onCameraMove
  11. ・ ・ ・
  12. Stop dragging
  13. ~~ onCameraChange ~~ ← End of movement by animateCamera?
  14. onCameraMove
  15. onCameraMove
  16. ~~ onCameraChange ~~ ← End of movement by dragging?
  17. onCameraIdle

If you drag while moving the map, ʻonCameraMoveCanceled`` will notify you of the interruption and immediately notify you of ʻonCameraMoveStarted as a new camera move. It is after that that `ʻanimateCamera is notified of the animation cancellation` ʻanimateCamera_onCancel``.

After that, ʻonCameraMove`` by dragging occurred continuously, and when dragging was stopped, ~~ ʻonCameraChange ~~ was called twice, and `ʻonCameraIdle was called once at the end.

The following is a summary of points to note in this case.

When streaming multiple events with RxJava etc., you have to be aware of the order and number of events that occur, but I feel that it may be a problem at that time.

Call stopAnimation when non-animated

  1. ~~onCameraChange~~
  2. onCameraIdle

Just calling stopAnimation will call ~~ ʻonCameraChange`` ~~, ʻonCameraIdle``. It feels bad.

Summary

[Bonus] Google Maps SDK for iOS

In the Google Maps SDK for iOS, our sister library, camera-related events

It is in. according to this,

there is. It's annoying because it's for Android. .. ..

Recommended Posts

About the camera change event of Google Maps Android API
Explains JavaScript of Google Maps Geocoding API
Note that the Google Maps Android API GoogleMap.getProjection is not a singleton
Create a tweet heatmap with the Google Maps API
Play music by hitting the unofficial API of Google Play Music
About the ease of Python
Change the theme of Jupyter
Change the style of matplotlib
About the components of Luigi
Beginners of Google Maps API and Twitter API made "tweet map"
About the features of Python
The story of creating a database using the Google Analytics API
About the SystemChannels API to take advantage of Flutter platform-specific features
Python beginners hit the unofficial API of Google Play Music to play music
Think about the minimum change problem
Change the background of Ubuntu (GNOME)
[Python] Hit the Google Translation API
About the return value of pthread_mutex_init ()
Change the Python version of Homebrew
About the return value of the histogram.
About the basic type of Go
About the upper limit of threads-max
Customize infoWindow for google maps for Android
Change the suffix of django-filter / DateFromToRangeFilter
About the behavior of yield_per of SqlAlchemy
About the size of matplotlib points
About the basics list of Python basics
[Android] Display images on the web in the info Window of Google Map
The google search console sitemap api client is in webmasters instead of search console
Judge the authenticity of posted articles by machine learning (Google Prediction API).
Change the length of Python csv strings
Script to change the description of fasta
About the behavior of enable_backprop of Chainer v2
About the virtual environment of python version 3.7
Get the minutes of the Diet via API
About the arguments of the setup function of PyCaret
About the Normal Equation of Linear Regression
Change the order of PostgreSQL on Heroku
Get holidays with the Google Calendar API
Change the destination batfish server of pybatfish
About the problem that the python version of Google App Engine does not mesh