इवेंट मैनेज करना

इस उदाहरण में, मैप पर कुछ इवेंट सुनने और उन्हें मैनेज करने का तरीका बताया गया है.

ज़्यादा जानकारी के लिए, दस्तावेज़ देखें.

शुरू करें

सैंपल कोड को आज़माने से पहले, आपको अपना डेवलपमेंट एनवायरमेंट कॉन्फ़िगर करना होगा. ज़्यादा जानकारी के लिए, Android कोड सैंपल के लिए Maps SDK टूल देखें.

कोड देखें

Kotlin



class EventsDemoActivity : AppCompatActivity(), 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)
    }

    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 AppCompatActivity
        implements OnMapClickListener, OnMapLongClickListener, OnCameraIdleListener,
        OnMapReadyCallback {

    private TextView tapTextView;
    private TextView cameraTextView;
    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.events_demo);

        tapTextView = findViewById(R.id.tap_text);
        cameraTextView = findViewById(R.id.camera_text);

        SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @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 में सैंपल प्रोजेक्ट इंपोर्ट करें:

  1. Android Studio में, फ़ाइल > नया > प्रोजेक्ट इंपोर्ट करें चुनें.
  2. उस जगह पर जाएं जहां आपने रिपॉज़िटरी को सेव किया है और Kotlin या Java के लिए प्रोजेक्ट डायरेक्ट्री चुनें:

    • कोटलिन: PATH-REPO/android-samples/ApiDemos/kotlin
    • Java: PATH-REPO/android-samples/ApiDemos/java
  3. खोलें को चुनें. Android Studio, Gradle बिल्ड टूल इस्तेमाल करके आपका प्रोजेक्ट बनाता है.
  4. अपने प्रोजेक्ट की local.properties फ़ाइल वाली डायरेक्ट्री में ही, एक खाली secrets.properties फ़ाइल बनाएं. ज़्यादा जानकारी के लिए, प्रोजेक्ट में अपनी एपीआई कुंजी जोड़ना लेख पढ़ें.
  5. नीचे दी गई स्ट्रिंग को secrets.properties में जोड़ें और YOUR_API_KEY को अपनी एपीआई कुंजी की वैल्यू से बदलें:

    MAPS_API_KEY=YOUR_API_KEY
  6. ऐप्लिकेशन चलाएं.