Handle events

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:

  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 .
  5. Get an API key from your project with the Maps SDK for Android enabled .
  6. Add the following string to secrets.properties , replacing YOUR_API_KEYwith the value of your API key:

    MAPS_API_KEY= YOUR_API_KEY 
    
  7. Run the app.
Create a Mobile Website
View Site in Mobile | Classic
Share by: