ตัวอย่างนี้แสดงวิธีฟังและจัดการเหตุการณ์บางอย่างบนแผนที่
ดูข้อมูลเพิ่มเติมได้ที่เอกสารประกอบ
เริ่มต้นใช้งาน
คุณต้องกําหนดค่าสภาพแวดล้อมการพัฒนาก่อนจึงจะลองใช้โค้ดตัวอย่างได้ ดูข้อมูลเพิ่มเติมได้ที่ตัวอย่างโค้ด Maps SDK สำหรับ Android
ดูโค้ด
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
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 )); } }
โคลนและเรียกใช้ตัวอย่าง
คุณต้องใช้ 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
- Kotlin:
- เลือกเปิด Android Studio จะสร้างโปรเจ็กต์โดยใช้เครื่องมือบิลด์ Gradle
- สร้างไฟล์
secrets.propertiesว่างในไดเรกทอรีเดียวกับไฟล์local.propertiesของโปรเจ็กต์ ดูข้อมูลเพิ่มเติมเกี่ยวกับไฟล์นี้ได้ที่หัวข้อเพิ่มคีย์ API ลงในโปรเจ็กต์ - รับคีย์ API จากโปรเจ็กต์ที่เปิดใช้ Maps SDK สำหรับ Android
เพิ่มสตริงต่อไปนี้ลงใน
secrets.propertiesโดยแทนที่ YOUR_API_KEY ด้วยค่าของคีย์ APIMAPS_API_KEY=YOUR_API_KEY- เรียกใช้แอป