AI-generated Key Takeaways
-
This example demonstrates how to listen for and respond to map events like clicks, long presses, and camera movements in your Android application.
-
Code samples are provided in both Kotlin and Java, illustrating the implementation of event handling for map interactions.
-
To run the sample code locally, you'll need to clone the repository, import the project into Android Studio, and configure your development environment with an API key.
-
The sample code showcases how to register event listeners for clicks, long presses, and camera idleness, allowing you to trigger actions based on user interactions with the map.
This example shows how to listen to and handle some events on the map.
For more information, see the documentation.
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.
View the code
Kotlin
class EventsDemoActivity : SamplesBaseActivity(), OnMapClickListener, OnMapLongClickListener, OnCameraIdleListener, 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<View?>(R.id.map_container)) } override fun onMapReady(googleMap: GoogleMap) { // return early if the map was not initialised properly map = googleMap map.setOnMapClickListener(this) map.setOnMapLongClickListener(this) map.setOnCameraIdleListener(this) } override fun onMapClick(point: LatLng) { tapTextView.text = "tapped, point=$point" } override fun onMapLongClick(point: LatLng) { tapTextView.text = "long pressed, point=$point" } override fun onCameraIdle() { if (!::map.isInitialized) return cameraTextView.text = map.cameraPosition.toString() } }
Java
public class EventsDemoActivity extends SamplesBaseActivity implements OnMapClickListener, OnMapLongClickListener, OnCameraIdleListener, 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.setOnCameraIdleListener(this); } @Override public void onMapClick(LatLng point) { tapTextView.setText("tapped, point=" + point); } @Override public void onMapLongClick(LatLng point) { tapTextView.setText("long pressed, point=" + point); } @Override public void onCameraIdle() { cameraTextView.setText(map.getCameraPosition().toString()); } }
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:
- In Android Studio, select File > New > Import Project.
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
- Kotlin:
- Select Open. Android Studio builds your project, using the Gradle build tool.
- Create a blank
secrets.properties
file in the same directory as your project'slocal.properties
file. For more information about this file, see Add your API key to the project. - Get an API key from your project with the Maps SDK for Android enabled.
Add the following string to
secrets.properties
, replacing YOUR_API_KEY with the value of your API key:MAPS_API_KEY=YOUR_API_KEY
- Run the app.