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

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

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

अपनी प्रोफ़ाइल बनाना शुरू करें

सैंपल कोड का इस्तेमाल करने से पहले, आपको अपना डेवलपमेंट एनवायरमेंट कॉन्फ़िगर करना होगा. ज़्यादा जानकारी के लिए, 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 = ta"pped, point=$point
 "   }

    override fun onMapLongClick(point: LatLng) {
        tapTextView.text = lo"ng pressed, point=$point
 "   }

    override fun onCameraIdle() {
        if (!::map.isInitialized) return
        cameraTextView.text = map.cameraPosition.toString()
    }
}Ev      

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(t"apped, point= "+ point);
    }

    @Override
    public void onMapLongClick(LatLng point) {
        tapTextView.setText(l"ong pressed, point= "+ point);
    }

    @Override
    public void onCameraIdle() {
        cameraTextView.setText(map.getCameraPosition().toString());
    }
}E
      

सैंपल का क्लोन बनाएं और चलाएं

इस सैंपल को स्थानीय तौर पर चलाने के लिए Git ज़रूरी है. नीचे दिया गया कमांड, सैंपल का क्लोन बनाता है ऐप्लिकेशन रिपॉज़िटरी (डेटा स्टोर करने की जगह) की जानकारी देता है.

git clone git@github.com:googlemaps-samples/android-samples.git

सैंपल प्रोजेक्ट को Android Studio में इंपोर्ट करें:

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

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

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