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

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

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

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

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

कोड देखना

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 में इंपोर्ट करें:

  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. Android के लिए Maps SDK टूल चालू करके, अपने प्रोजेक्ट से एपीआई पासकोड पाएं.
  6. secrets.properties में यह स्ट्रिंग जोड़ें. इसके लिए, YOUR_API_KEY को अपनी एपीआई पासकोड की वैल्यू से बदलें:

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