Using the Maps SDK for Android, you can listen to events on the map.
Code samples
The ApiDemos repository on GitHub includes samples that demonstrates events and listeners:
- EventsDemoActivity: Map click and camera change events
- CameraDemoActivity: Camera change events
- CircleDemoActivity: Marker click and drag events
- GroundOverlayDemoActivity: Ground overlay click events
- IndoorDemoActivity: Indoor map events
- MarkerDemoActivity: Marker and info window events
- PolygonDemoActivity: Polygon events
Map click / long click events
If you want to respond to a user tapping on a point on the map, you can use an
OnMapClickListener
which you can set on the map by
calling GoogleMap.setOnMapClickListener(OnMapClickListener)
. When a user
clicks (taps) somewhere on the map, you will receive an onMapClick(LatLng)
event that indicates the location on the map that the user clicked. Note that
if you need the corresponding location on the screen (in pixels), you can
obtain a Projection
from the map which allows you to convert
between latitude/longitude coordinates and screen pixel coordinates.
You can also listen for long click events with an
OnMapLongClickListener
which you can set on the
map by calling GoogleMap.setOnMapLongClickListener(OnMapLongClickListener)
.
This listener behaves similarly to the click listener and will be notified on
long click events with an onMapLongClick(LatLng)
callback.
Disabling click events in lite mode
To disable click events on a map in lite mode, call setClickable()
on the view that contains the MapView
or MapFragment
. This is useful,
for example, when displaying a map or maps in a list view, where you want
the click event to invoke an action unrelated to the map.
The option to disable click events is available in lite mode only. Disabling click events will also make markers non-clickable. It will not affect other controls on the map.
For a MapView
:
MapView view;
...
view.setClickable(false);
For a MapFragment
:
MapFragment fragment;
...
fragment.getView().setClickable(false);
Camera change events
The map view is modeled as a camera looking down on a flat plane. You can change the properties of the camera to affect the zoom level, view port and perspective of the map. See the guide to the camera. Users can also affect the camera by making gestures.
Using camera change listeners, you can keep track of camera movements. Your app can receive notifications 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.
The following sample illustrates all the available camera event listeners:
public class MyCameraActivity extends FragmentActivity implements
OnCameraMoveStartedListener,
OnCameraMoveListener,
OnCameraMoveCanceledListener,
OnCameraIdleListener,
OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_camera);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
mMap.setOnCameraIdleListener(this);
mMap.setOnCameraMoveStartedListener(this);
mMap.setOnCameraMoveListener(this);
mMap.setOnCameraMoveCanceledListener(this);
// Show Sydney on the map.
mMap.moveCamera(CameraUpdateFactory
.newLatLngZoom(new LatLng(-33.87365, 151.20689), 10));
}
@Override
public void onCameraMoveStarted(int reason) {
if (reason == OnCameraMoveStartedListener.REASON_GESTURE) {
Toast.makeText(this, "The user gestured on the map.",
Toast.LENGTH_SHORT).show();
} else if (reason == OnCameraMoveStartedListener
.REASON_API_ANIMATION) {
Toast.makeText(this, "The user tapped something on the map.",
Toast.LENGTH_SHORT).show();
} else if (reason == OnCameraMoveStartedListener
.REASON_DEVELOPER_ANIMATION) {
Toast.makeText(this, "The app moved the camera.",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCameraMove() {
Toast.makeText(this, "The camera is moving.",
Toast.LENGTH_SHORT).show();
}
@Override
public void onCameraMoveCanceled() {
Toast.makeText(this, "Camera movement canceled.",
Toast.LENGTH_SHORT).show();
}
@Override
public void onCameraIdle() {
Toast.makeText(this, "The camera has stopped moving.",
Toast.LENGTH_SHORT).show();
}
}
The following camera listeners are available:
The
onCameraMoveStarted()
callback of theOnCameraMoveStartedListener
is invoked when the camera starts moving. The callback method receives areason
for the camera motion. The reason can be one of the following:REASON_GESTURE
indicates that the camera moved in response to a user's gesture on the map, such as panning, tilting, pinching to zoom, or rotating the map.REASON_API_ANIMATION
indicates that the API has moved the camera in response to a non-gesture user action, such as tapping the zoom button, tapping the My Location button, or clicking a marker.REASON_DEVELOPER_ANIMATION
indicates that your app has initiated the camera movement.
The
onCameraMove()
callback of theOnCameraMoveListener
is invoked multiple times while the camera is moving or the user is interacting with the touch screen. As a guide to how often the callback is invoked, it's useful to know that the API invokes the callback once per frame. Note, however, that this callback is invoked asynchronously and is therefore out of synch with what is visible on the screen. Also note that it's possible for the camera position to remain unchanged between oneonCameraMove()
callback and the next.The
OnCameraIdle()
callback of theOnCameraIdleListener
is invoked when the camera stops moving and the user has stopped interacting with the map.The
OnCameraMoveCanceled()
callback of theOnCameraMoveCanceledListener
is invoked when the current camera movement has been interrupted. Immediately after theOnCameraMoveCanceled()
callback, theonCameraMoveStarted()
callback is invoked with the newreason
.If your app explicitly calls
GoogleMap.stopAnimation()
, theOnCameraMoveCanceled()
callback is invoked, but theonCameraMoveStarted()
callback is not invoked.
To set a listener on the map, call the relevant set-listener method.
For example, to request a callback from the OnCameraMoveStartedListener
, call
GoogleMap.setOnCameraMoveStartedListener()
.
You can get the camera's target (latitude/longitude), zoom, bearing and tilt
from the CameraPosition
. See the guide to
camera position for details about these properties.
Events on businesses and other points of interest
By default, points of interest (POIs) appear on the base map along with their corresponding icons. POIs include parks, schools, government buildings, and more, as well as business POIs such as shops, restaurants, and hotels.
You can respond to click events on a POI. See the guide to businesses and other points of interest.
Indoor map events
You can use events to find and customize the active level of an indoor map. Use
the OnIndoorStateChangeListener
interface to set a listener to be called when
either a new building is focused or a new level is activated in a building.
Get the building that is currently in focus by calling
GoogleMap.getFocusedBuilding()
.
Centering the map on a specific lat/long will
generally give you the building at that lat/long, but this is not guaranteed.
You can then find the currently active level by calling
IndoorBuilding.getActiveLevelIndex()
.
IndoorBuilding building = mMap.getFocusedBuilding(); if (building == null) { return null; } return building.getLevels().get(building.getActiveLevelIndex());
This is useful if you want to show custom markup for the active level, such as markers, ground overlays, tile overlays, polygons, polylines, and other shapes.
Hint: To go back to street level, get the default level via
IndoorBuilding.getDefaultLevelIndex()
, and set it as the active level via
IndoorLevel.activate()
.
Marker and info window events
You can listen and respond to marker events, including marker click and drag
events, by setting the corresponding listener on the GoogleMap
object to which
the marker belongs. See the guide to marker events.
You can also listen to events on info windows.
Shape and overlay events
You can listen and respond to click events on polylines, polygons, circles, and ground overlays.
Location events
Your app can respond to the following events related to the My Location layer:
- If the user clicks the My Location button, your app receives an
onMyLocationButtonClick()
callback from theGoogleMap.OnMyLocationButtonClickListener
. - If the user clicks the My Location blue dot, your app receives an
onMyLocationClick()
callback from theGoogleMap.OnMyLocationClickListener
.
For details, see the guide to the My Location layer.