How to Detect a User Interaction on Google Maps View in Android?

Matso Abgaryan
Feb 10 · 2 min read

The Android MapView is great in many ways, but it is missing some handy user interaction listeners implementations. That is why the MapView APIs are incomplete.

Let’s say we would like to track user interaction with the map in those cases :

When the user

  • Starts Zooming in on the map
  • Stops Zooming in on the map
  • Starts Zooming out the map
  • Stops Zooming out the map
  • Rotates the map
  • Pans the map

Those map interactions seem very simple and interesting for the analytics team to track.
As you can guess, it is not straightforward to implement these elementary user interactions, or at least I could not find one.

To be able to implement those map interactions above we need to use 3 MapView Listener :

1.

map.setOnCameraMoveStartedListener { reason ->}

Which gets called every time the map starts moving, zooming in/out, or the camera angle change, and where the reason tells us who started to move the camera and has 3 values:

REASON_GESTURE,

REASON_API_ANIMATION,

REASON_DEVELOPER_ANIMATION

We will need to use the only REASON_GESTURE for the user interaction.

2.

map.setOnCameraMoveListener {}

Which gets called every time the map moves, zooms in/out, or the camera angle changes.

3.

map.setOnCameraIdleListener {}

This Listener called when the camera is in an idle state.

First, let’s define an interface for the user interactions which has 6 methods for the interactions cases mentioned above.

Then, let’s create an implementation class for the user interactions tracking.

Finally, let’s create a function for setting the MapInteractions listener and implementing the user interaction logic.

The Startup

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store