จัดการเหตุการณ์
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ตัวอย่างนี้แสดงวิธีฟังและจัดการเหตุการณ์บางอย่างบนแผนที่
ดูข้อมูลเพิ่มเติมได้ที่เอกสารประกอบ
เริ่มต้นใช้งาน
คุณต้องกําหนดค่าสภาพแวดล้อมการพัฒนาก่อนจึงจะลองใช้โค้ดตัวอย่างได้
ดูข้อมูลเพิ่มเติมได้ที่ตัวอย่างโค้ด Maps SDK สำหรับ Android
ดูโค้ด
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());
}
}
โคลนและเรียกใช้ตัวอย่าง
คุณต้องใช้ Git เพื่อเรียกใช้ตัวอย่างนี้ในเครื่อง คำสั่งต่อไปนี้จะโคลนที่เก็บแอปพลิเคชันตัวอย่าง
git clone git@github.com:googlemaps-samples/android-samples.git
นําเข้าโปรเจ็กต์ตัวอย่างไปยัง Android Studio โดยทำดังนี้
- ใน Android Studio ให้เลือกไฟล์ > ใหม่ > นำเข้าโปรเจ็กต์
ไปที่ตำแหน่งที่คุณบันทึกที่เก็บไว้ แล้วเลือกไดเรกทอรีโปรเจ็กต์สำหรับ Kotlin หรือ Java
- Kotlin:
PATH-REPO/android-samples/ApiDemos/kotlin
- Java:
PATH-REPO/android-samples/ApiDemos/java
- เลือกเปิด Android Studio จะสร้างโปรเจ็กต์โดยใช้เครื่องมือบิลด์ Gradle
- สร้างไฟล์
secrets.properties
ว่างในไดเรกทอรีเดียวกับไฟล์ local.properties
ของโปรเจ็กต์ ดูข้อมูลเพิ่มเติมเกี่ยวกับไฟล์นี้ได้ที่หัวข้อเพิ่มคีย์ API ลงในโปรเจ็กต์
- รับคีย์ API จากโปรเจ็กต์ที่เปิดใช้ Maps SDK สำหรับ Android
เพิ่มสตริงต่อไปนี้ลงใน secrets.properties
โดยแทนที่ YOUR_API_KEY ด้วยค่าของคีย์ API
MAPS_API_KEY=YOUR_API_KEY
- เรียกใช้แอป
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eThis example demonstrates how to listen for and respond to map events like clicks, long presses, and camera movements in your Android application.\u003c/p\u003e\n"],["\u003cp\u003eCode samples are provided in both Kotlin and Java, illustrating the implementation of event handling for map interactions.\u003c/p\u003e\n"],["\u003cp\u003eTo 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.\u003c/p\u003e\n"],["\u003cp\u003eThe 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.\u003c/p\u003e\n"]]],[],null,["This example shows how to listen to and handle some events on the map.\n\nFor more information, see the [documentation.](/maps/documentation/android-sdk/events)\n\nGet started\n\nBefore you can try the sample code, you must configure your development environment.\nFor more information, see [Maps SDK for Android code samples](/maps/documentation/android-sdk/examples).\n\nView the code\n\n\nKotlin \n\n```kotlin\nclass EventsDemoActivity : SamplesBaseActivity(), OnMapClickListener,\n OnMapLongClickListener, OnCameraIdleListener, OnMapReadyCallback {\n\n private lateinit var tapTextView: TextView\n private lateinit var cameraTextView: TextView\n private lateinit var map: GoogleMap\n\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.events_demo)\n tapTextView = findViewById(R.id.tap_text)\n cameraTextView = findViewById(R.id.camera_text)\n val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?\n mapFragment?.getMapAsync(this)\n applyInsets(findViewById\u003cView?\u003e(R.id.map_container))\n }\n\n override fun onMapReady(googleMap: GoogleMap) {\n // return early if the map was not initialised properly\n map = googleMap\n map.setOnMapClickListener(this)\n map.setOnMapLongClickListener(this)\n map.setOnCameraIdleListener(this)\n }\n\n override fun onMapClick(point: LatLng) {\n tapTextView.text = \"tapped, point=$point\"\n }\n\n override fun onMapLongClick(point: LatLng) {\n tapTextView.text = \"long pressed, point=$point\"\n }\n\n override fun onCameraIdle() {\n if (!::map.isInitialized) return\n cameraTextView.text = map.cameraPosition.toString()\n }\n}https://github.com/googlemaps-samples/android-samples/blob/73ac16ffca01a2ca6fedd1988c713138ec000082/ApiDemos/project/kotlin-app/src/main/java/com/example/kotlindemos/EventsDemoActivity.kt#L31-L68\n\n \n```\n\nJava \n\n```java\npublic class EventsDemoActivity extends SamplesBaseActivity\n implements OnMapClickListener, OnMapLongClickListener, OnCameraIdleListener,\n OnMapReadyCallback {\n\n private TextView tapTextView;\n private TextView cameraTextView;\n private GoogleMap map;\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(com.example.common_ui.R.layout.events_demo);\n\n tapTextView = findViewById(com.example.common_ui.R.id.tap_text);\n cameraTextView = findViewById(com.example.common_ui.R.id.camera_text);\n\n SupportMapFragment mapFragment =\n (SupportMapFragment) getSupportFragmentManager().findFragmentById(com.example.common_ui.R.id.map);\n mapFragment.getMapAsync(this);\n applyInsets(findViewById(com.example.common_ui.R.id.map_container));\n }\n\n @Override\n public void onMapReady(GoogleMap map) {\n this.map = map;\n this.map.setOnMapClickListener(this);\n this.map.setOnMapLongClickListener(this);\n this.map.setOnCameraIdleListener(this);\n }\n\n @Override\n public void onMapClick(LatLng point) {\n tapTextView.setText(\"tapped, point=\" + point);\n }\n\n @Override\n public void onMapLongClick(LatLng point) {\n tapTextView.setText(\"long pressed, point=\" + point);\n }\n\n @Override\n public void onCameraIdle() {\n cameraTextView.setText(map.getCameraPosition().toString());\n }\n}https://github.com/googlemaps-samples/android-samples/blob/73ac16ffca01a2ca6fedd1988c713138ec000082/ApiDemos/project/java-app/src/main/java/com/example/mapdemo/EventsDemoActivity.java#L35-L79\n\n \n```\n\n\u003cbr /\u003e\n\nClone and run the samples\n\nGit is required to run this sample locally. The following command clones the sample\napplication repository. \n\n```\ngit clone git@github.com:googlemaps-samples/android-samples.git\n```\n\nImport the sample project into Android Studio:\n\n1. In Android Studio, select **File \\\u003e New \\\u003e Import Project**.\n2. Go to the location where you saved the repository and select the project directory for\n Kotlin or Java:\n\n - **Kotlin** : \u003cvar translate=\"no\"\u003ePATH-REPO\u003c/var\u003e`/android-samples/ApiDemos/kotlin`\n - **Java** : \u003cvar translate=\"no\"\u003ePATH-REPO\u003c/var\u003e`/android-samples/ApiDemos/java`\n3. Select **Open**. Android Studio builds your project, using the Gradle build tool.\n4. 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](/maps/documentation/android-sdk/config#step_3_add_your_api_key_to_the_project).\n5. [Get an API key](/maps/documentation/android-sdk/get-api-key) from your project with the [Maps SDK for Android enabled](/maps/documentation/android-sdk/cloud-setup#enabling-apis).\n6. Add the following string to `secrets.properties`, replacing **YOUR_API_KEY** with the value of\n your API key:\n\n ```scdoc\n MAPS_API_KEY=YOUR_API_KEY\n ```\n7. Run the app."]]