This example shows how to listen to and handle some events on the map.

For more information, see [Events](https://developers.google.com/maps/documentation/android-sdk/events.md.txt).

## Get started

Before you can try the sample code, you must configure your development
environment. For more information, see
[Maps SDK for Android code samples](https://developers.google.com/maps/documentation/android-sdk/examples.md.txt).

## View the code


### Kotlin

```kotlin
class EventsDemoActivity :
    SamplesBaseActivity(),
    OnMapClickListener,
    OnMapLongClickListener,
    OnCameraIdleListener,
    OnCameraMoveListener,
    OnMapReadyCallback {

    private lateinit var tapTextView: TextView
    private lateinit var cameraTextView: TextView
    private lateinit var map: GoogleMap

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.events_demo)
        tapTextView = findViewById(R.id.tap_text)
        cameraTextView = findViewById(R.id.camera_text)
        val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
        mapFragment?.getMapAsync(this)
        applyInsets(findViewById(R.id.map_container))
    }

    override fun onMapReady(googleMap: GoogleMap) {
        map = googleMap
        map.setOnMapClickListener(this)
        map.setOnMapLongClickListener(this)
        map.setOnCameraMoveListener(this)
        map.setOnCameraIdleListener(this)
        updateCameraPosition()
    }

    override fun onMapClick(point: LatLng) {
        val lat = String.format(Locale.US, "%.6f", point.latitude)
        val lng = String.format(Locale.US, "%.6f", point.longitude)
        tapTextView.text = getString(R.string.events_tapped_format, lat, lng)
    }

    override fun onMapLongClick(point: LatLng) {
        val lat = String.format(Locale.US, "%.6f", point.latitude)
        val lng = String.format(Locale.US, "%.6f", point.longitude)
        tapTextView.text = getString(R.string.events_long_pressed_format, lat, lng)
    }

    override fun onCameraMove() {
        updateCameraPosition()
    }

    override fun onCameraIdle() {
        updateCameraPosition()
    }

    private fun updateCameraPosition() {
        if (!::map.isInitialized) return
        val pos = map.cameraPosition
        val lat = String.format(Locale.US, "%.6f", pos.target.latitude)
        val lng = String.format(Locale.US, "%.6f", pos.target.longitude)
        val zoom = String.format(Locale.US, "%.1f", pos.zoom)
        val tilt = String.format(Locale.US, "%.1f", pos.tilt)
        val bearing = String.format(Locale.US, "%.1f", pos.bearing)
        cameraTextView.text = getString(R.string.events_camera_position_format, lat, lng, zoom, tilt, bearing)
    }
}

      
```

### Java

```java
public class EventsDemoActivity extends SamplesBaseActivity
        implements OnMapClickListener,
                OnMapLongClickListener,
                OnCameraIdleListener,
                GoogleMap.OnCameraMoveListener,
                OnMapReadyCallback {

    private TextView tapTextView;
    private TextView cameraTextView;
    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.example.common_ui.R.layout.events_demo);

        tapTextView = findViewById(com.example.common_ui.R.id.tap_text);
        cameraTextView = findViewById(com.example.common_ui.R.id.camera_text);

        SupportMapFragment mapFragment =
                (SupportMapFragment)
                        getSupportFragmentManager()
                                .findFragmentById(com.example.common_ui.R.id.map);
        mapFragment.getMapAsync(this);
        applyInsets(findViewById(com.example.common_ui.R.id.map_container));
    }

    @Override
    public void onMapReady(GoogleMap map) {
        this.map = map;
        this.map.setOnMapClickListener(this);
        this.map.setOnMapLongClickListener(this);
        this.map.setOnCameraMoveListener(this);
        this.map.setOnCameraIdleListener(this);
        updateCameraPosition();
    }

    @Override
    public void onMapClick(LatLng point) {
        String lat = String.format(Locale.US, "%.6f", point.latitude);
        String lng = String.format(Locale.US, "%.6f", point.longitude);
        tapTextView.setText(
                getString(com.example.common_ui.R.string.events_tapped_format, lat, lng));
    }

    @Override
    public void onMapLongClick(LatLng point) {
        String lat = String.format(Locale.US, "%.6f", point.latitude);
        String lng = String.format(Locale.US, "%.6f", point.longitude);
        tapTextView.setText(
                getString(com.example.common_ui.R.string.events_long_pressed_format, lat, lng));
    }

    @Override
    public void onCameraMove() {
        updateCameraPosition();
    }

    @Override
    public void onCameraIdle() {
        updateCameraPosition();
    }

    private void updateCameraPosition() {
        if (map == null) return;
        com.google.android.gms.maps.model.CameraPosition pos = map.getCameraPosition();
        String lat = String.format(Locale.US, "%.6f", pos.target.latitude);
        String lng = String.format(Locale.US, "%.6f", pos.target.longitude);
        String zoom = String.format(Locale.US, "%.1f", pos.zoom);
        String tilt = String.format(Locale.US, "%.1f", pos.tilt);
        String bearing = String.format(Locale.US, "%.1f", pos.bearing);
        cameraTextView.setText(
                getString(
                        com.example.common_ui.R.string.events_camera_position_format,
                        lat,
                        lng,
                        zoom,
                        tilt,
                        bearing));
    }
}

      
```

<br />

## Clone and run the samples

Git is required to run this sample locally. The following command clones the sample
application repository.

```
git clone git@github.com:googlemaps-samples/android-samples.git
```

Import the sample project into Android Studio:

1. In Android Studio, select **File \> New \> Import Project**.
2. Go to the location where you saved the repository and select the project directory for
   Kotlin or Java:

   - **Kotlin** : `PATH-REPO/android-samples/ApiDemos/kotlin`
   - **Java** : `PATH-REPO/android-samples/ApiDemos/java`
3. Select **Open**. Android Studio builds your project, using the Gradle build tool.
4. Create a blank `secrets.properties` file in the same directory as your project's `local.properties` file. For more information about this file, see [Add your API key to the project](https://developers.google.com/maps/documentation/android-sdk/config.md.txt#step_3_add_your_api_key_to_the_project).
5. [Get an API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key.md.txt) from your project with the [Maps SDK for Android enabled](https://developers.google.com/maps/documentation/android-sdk/cloud-setup#enabling-apis).
6. Add the following string to `secrets.properties`, replacing **YOUR_API_KEY** with the value of
   your API key:

   ```
   MAPS_API_KEY=YOUR_API_KEY
   ```
7. Run the app.