Bu örnekte, haritada bazı etkinliklerin nasıl dinleneceği ve işleneceği gösterilmektedir.
Daha fazla bilgi için dokümanları inceleyin.
Başlayın
Örnek kodu denemeden önce geliştirme ortamınızı yapılandırmanız gerekir. Daha fazla bilgi için Android için Haritalar SDK'sı kod örnekleri başlıklı makaleyi inceleyin.
Kodu görüntüleme
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()); } }
Örnekleri klonlama ve çalıştırma
Bu örneği yerel olarak çalıştırmak için Git gereklidir. Aşağıdaki komut, örnek uygulama deposunu klonlar.
git clone git@github.com:googlemaps-samples/android-samples.git
Örnek projeyi Android Studio'ya aktarın:
- Android Studio'da Dosya > Yeni > Projeyi İçe Aktar'ı seçin.
Depoyu kaydettiğiniz konuma gidin ve Kotlin veya Java için proje dizinini seçin:
- Kotlin:
PATH-REPO/android-samples/ApiDemos/kotlin
- Java:
PATH-REPO/android-samples/ApiDemos/java
- Kotlin:
- Aç'ı seçin. Android Studio, Gradle derleme aracını kullanarak projenizi derleyin.
- Projenizin
local.properties
dosyasıyla aynı dizinde boş birsecrets.properties
dosyası oluşturun. Bu dosya hakkında daha fazla bilgi için API anahtarınızı projeye ekleme başlıklı makaleyi inceleyin. - Android için Haritalar SDK'sı etkin olan projenizden API anahtarı alın.
secrets.properties
değerine aşağıdaki dizeyi ekleyin. YOUR_API_KEY değerini API anahtarınızın değeriyle değiştirin:MAPS_API_KEY=YOUR_API_KEY
- Uygulamayı çalıştırın.