इवेंट

प्लैटफ़ॉर्म चुनें: Android iOS JavaScript

Android के लिए Maps SDK टूल का इस्तेमाल करके, मैप पर इवेंट सुने जा सकते हैं.

कोड सैंपल

GitHub पर ApiDemos रिपॉज़िटरी में, ऐसे सैंपल शामिल होते हैं जो इवेंट और लिसनर की जानकारी देते हैं:

कोटलिन

Java

मैप क्लिक / लंबी क्लिक वाले इवेंट

अगर आप मैप के किसी पॉइंट पर टैप करने वाले उपयोगकर्ता को जवाब देना चाहते हैं, तो आपके पास OnMapClickListener का इस्तेमाल करने का विकल्प है. इसे GoogleMap.setOnMapClickListener(OnMapClickListener) पर कॉल करके मैप पर सेट किया जा सकता है. जब कोई उपयोगकर्ता मैप पर कहीं पर क्लिक (टैप) करता है, तो आपको एक onMapClick(LatLng) इवेंट मिलेगा. यह इवेंट, मैप पर उस जगह की जानकारी देता है जिस पर उपयोगकर्ता ने क्लिक किया है. ध्यान दें कि अगर आपको स्क्रीन पर संबंधित जगह (पिक्सल में) की ज़रूरत है, तो मैप पर Projection पाएं. इससे आपको अक्षांश/देशांतर निर्देशांक और स्क्रीन पिक्सल निर्देशांक के बीच कन्वर्ट करने की सुविधा मिलेगी.

OnMapLongClickListener की मदद से, लंबे क्लिक वाले इवेंट भी सुने जा सकते हैं. GoogleMap.setOnMapLongClickListener(OnMapLongClickListener) को कॉल करके, इस इवेंट को मैप पर सेट किया जा सकता है. यह लिसनर, क्लिक लिसनर की तरह ही काम करता है. इसे onMapLongClick(LatLng) कॉलबैक से, लंबे क्लिक वाले इवेंट के बारे में सूचना दी जाएगी.

लाइट मोड में क्लिक इवेंट की सुविधा बंद करना

लाइट मोड में मैप पर क्लिक इवेंट को बंद करने के लिए, MapView या MapFragment वाले व्यू पर setClickable() को कॉल करें. यह उपयोगी होता है, उदाहरण के लिए, किसी सूची दृश्य में मैप या मैप दिखाते समय, जहां आपको यह तय करना होता है कि क्लिक इवेंट, मैप से अलग कार्रवाई शुरू करे.

क्लिक इवेंट को बंद करने का विकल्प सिर्फ़ लाइट मोड में उपलब्ध है. क्लिक इवेंट को बंद करने से भी मार्कर पर क्लिक नहीं किया जा सकेगा. इससे मैप के दूसरे कंट्रोल पर कोई असर नहीं पड़ेगा.

MapView के लिए:

Kotlin



val mapView = findViewById<MapView>(R.id.mapView)
mapView.isClickable = false

      

Java


MapView mapView = findViewById(R.id.mapView);
mapView.setClickable(false);

      

MapFragment के लिए:

Kotlin



val mapFragment = supportFragmentManager
    .findFragmentById(R.id.map) as SupportMapFragment
val view = mapFragment.view
view?.isClickable = false

      

Java


SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
    .findFragmentById(R.id.map);
View view = mapFragment.getView();
view.setClickable(false);

      

कैमरा बदलने के इवेंट

मैप व्यू, कैमरे की तरह मॉडल किया गया है. यह किसी सपाट हवाई जहाज़ पर नीचे की ओर देखा जा रहा है. ज़ूम लेवल, पोर्ट देखने, और मैप के नज़रिए पर असर डालने के लिए, कैमरे की प्रॉपर्टी बदली जा सकती हैं. कैमरे के लिए गाइड देखें. उपयोगकर्ता भी हाथ के जेस्चर बनाकर, कैमरे पर असर डाल सकते हैं.

कैमरा बदलाव लिसनर का इस्तेमाल करके, कैमरे की गतिविधियों को ट्रैक किया जा सकता है. आपके ऐप्लिकेशन को, कैमरे से हलचल होने, उसके चालू होने, और उसके खत्म होने से जुड़े इवेंट के बारे में सूचनाएं मिल सकती हैं. यह भी देखा जा सकता है कि कैमरा क्यों चल रहा है. क्या ऐसा उपयोगकर्ता के जेस्चर, पहले से मौजूद एपीआई ऐनिमेशन या डेवलपर के कंट्रोल की गई गतिविधियों की वजह से हो रहा है.

यहां दिए गए सैंपल में, कैमरा इवेंट के सभी उपलब्ध लिसनर की जानकारी दी गई है:

Kotlin

/*
 * Copyright 2018 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.kotlindemos

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.CompoundButton
import android.widget.SeekBar
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.maps.CameraUpdate
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.GoogleMap.CancelableCallback
import com.google.android.gms.maps.GoogleMap.OnCameraIdleListener
import com.google.android.gms.maps.GoogleMap.OnCameraMoveCanceledListener
import com.google.android.gms.maps.GoogleMap.OnCameraMoveListener
import com.google.android.gms.maps.GoogleMap.OnCameraMoveStartedListener
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.PolylineOptions

/**
 * This shows how to change the camera position for the map.
 */
class CameraDemoActivity :
        AppCompatActivity(),
        OnCameraMoveStartedListener,
        OnCameraMoveListener,
        OnCameraMoveCanceledListener,
        OnCameraIdleListener,
        OnMapReadyCallback {
    /**
     * The amount by which to scroll the camera. Note that this amount is in raw pixels, not dp
     * (density-independent pixels).
     */
    private val SCROLL_BY_PX = 100
    private val TAG = CameraDemoActivity::class.java.name
    private val sydneyLatLng = LatLng(-33.87365, 151.20689)
    private val bondiLocation: CameraPosition = CameraPosition.Builder()
            .target(LatLng(-33.891614, 151.276417))
            .zoom(15.5f)
            .bearing(300f)
            .tilt(50f)
            .build()

    private val sydneyLocation: CameraPosition = CameraPosition.Builder().
            target(LatLng(-33.87365, 151.20689))
            .zoom(15.5f)
            .bearing(0f)
            .tilt(25f)
            .build()

    private lateinit var map: GoogleMap
    private lateinit var animateToggle: CompoundButton
    private lateinit var customDurationToggle: CompoundButton
    private lateinit var customDurationBar: SeekBar
    private var currPolylineOptions: PolylineOptions? = null
    private var isCanceled = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.camera_demo)
        animateToggle = findViewById(R.id.animate)
        customDurationToggle = findViewById(R.id.duration_toggle)
        customDurationBar = findViewById(R.id.duration_bar)

        updateEnabledState()

        val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)
    }

    override fun onResume() {
        super.onResume()
        updateEnabledState()
    }

    override fun onMapReady(googleMap: GoogleMap) {
        map = googleMap
        // return early if the map was not initialised properly
        with(googleMap) {
            setOnCameraIdleListener(this@CameraDemoActivity)
            setOnCameraMoveStartedListener(this@CameraDemoActivity)
            setOnCameraMoveListener(this@CameraDemoActivity)
            setOnCameraMoveCanceledListener(this@CameraDemoActivity)
            // We will provide our own zoom controls.
            uiSettings.isZoomControlsEnabled = false
            uiSettings.isMyLocationButtonEnabled = true

            // Show Sydney
            moveCamera(CameraUpdateFactory.newLatLngZoom(sydneyLatLng, 10f))
        }
    }

    /**
     * When the map is not ready the CameraUpdateFactory cannot be used. This should be used to wrap
     * all entry points that call methods on the Google Maps API.
     *
     * @param stuffToDo the code to be executed if the map is initialised
     */
    private fun checkReadyThen(stuffToDo: () -> Unit) {
        if (!::map.isInitialized) {
            Toast.makeText(this, R.string.map_not_ready, Toast.LENGTH_SHORT).show()
        } else {
            stuffToDo()
        }
    }

    /**
     * Called when the Go To Bondi button is clicked.
     */
    @Suppress("UNUSED_PARAMETER")
    fun onGoToBondi(view: View) {
        checkReadyThen {
            changeCamera(CameraUpdateFactory.newCameraPosition(bondiLocation))
        }
    }

    /**
     * Called when the Animate To Sydney button is clicked.
     */
    @Suppress("UNUSED_PARAMETER")
    fun onGoToSydney(view: View) {
        checkReadyThen {
            changeCamera(CameraUpdateFactory.newCameraPosition(sydneyLocation),
                    object : CancelableCallback {
                        override fun onFinish() {
                            Toast.makeText(baseContext, "Animation to Sydney complete",
                                    Toast.LENGTH_SHORT).show()
                        }

                        override fun onCancel() {
                            Toast.makeText(baseContext, "Animation to Sydney canceled",
                                    Toast.LENGTH_SHORT).show()
                        }
                    })
        }
    }

    /**
     * Called when the stop button is clicked.
     */
    @Suppress("UNUSED_PARAMETER")
    fun onStopAnimation(view: View) = checkReadyThen { map.stopAnimation() }

    /**
     * Called when the zoom in button (the one with the +) is clicked.
     */
    @Suppress("UNUSED_PARAMETER")
    fun onZoomIn(view: View) = checkReadyThen { changeCamera(CameraUpdateFactory.zoomIn()) }

    /**
     * Called when the zoom out button (the one with the -) is clicked.
     */
    @Suppress("UNUSED_PARAMETER")
    fun onZoomOut(view: View) = checkReadyThen { changeCamera(CameraUpdateFactory.zoomOut()) }

    /**
     * Called when the tilt more button (the one with the /) is clicked.
     */
    @Suppress("UNUSED_PARAMETER")
    fun onTiltMore(view: View) {
        checkReadyThen {

            val newTilt = Math.min(map.cameraPosition.tilt + 10, 90F)
            val cameraPosition = CameraPosition.Builder(map.cameraPosition).tilt(newTilt).build()

            changeCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
        }
    }

    /**
     * Called when the tilt less button (the one with the \) is clicked.
     */
    @Suppress("UNUSED_PARAMETER")
    fun onTiltLess(view: View) {
        checkReadyThen {

            val newTilt = Math.max(map.cameraPosition.tilt - 10, 0F)
            val cameraPosition = CameraPosition.Builder(map.cameraPosition).tilt(newTilt).build()

            changeCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
        }
    }

    /**
     * Called when the left arrow button is clicked. This causes the camera to move to the left
     */
    @Suppress("UNUSED_PARAMETER")
    fun onScrollLeft(view: View) {
        checkReadyThen {
            changeCamera(CameraUpdateFactory.scrollBy((-SCROLL_BY_PX).toFloat(),0f))
        }
    }

    /**
     * Called when the right arrow button is clicked. This causes the camera to move to the right.
     */
    @Suppress("UNUSED_PARAMETER")
    fun onScrollRight(view: View) {
        checkReadyThen {
            changeCamera(CameraUpdateFactory.scrollBy(SCROLL_BY_PX.toFloat(), 0f))
        }
    }

    /**
     * Called when the up arrow button is clicked. The causes the camera to move up.
     */
    @Suppress("UNUSED_PARAMETER")
    fun onScrollUp(view: View) {
        checkReadyThen {
            changeCamera(CameraUpdateFactory.scrollBy(0f, (-SCROLL_BY_PX).toFloat()))
        }
    }

    /**
     * Called when the down arrow button is clicked. This causes the camera to move down.
     */
    @Suppress("UNUSED_PARAMETER")
    fun onScrollDown(view: View) {
        checkReadyThen {
            changeCamera(CameraUpdateFactory.scrollBy(0f, SCROLL_BY_PX.toFloat()))
        }
    }

    /**
     * Called when the animate button is toggled
     */
    @Suppress("UNUSED_PARAMETER")
    fun onToggleAnimate(view: View) = updateEnabledState()

    /**
     * Called when the custom duration checkbox is toggled
     */
    @Suppress("UNUSED_PARAMETER")
    fun onToggleCustomDuration(view: View) = updateEnabledState()

    /**
     * Update the enabled state of the custom duration controls.
     */
    private fun updateEnabledState() {
        customDurationToggle.isEnabled = animateToggle.isChecked
        customDurationBar.isEnabled = animateToggle.isChecked && customDurationToggle.isChecked
    }

    /**
     * Change the camera position by moving or animating the camera depending on the state of the
     * animate toggle button.
     */
    private fun changeCamera(update: CameraUpdate, callback: CancelableCallback? = null) {
        if (animateToggle.isChecked) {
            if (customDurationToggle.isChecked) {
                // The duration must be strictly positive so we make it at least 1.
                map.animateCamera(update, Math.max(customDurationBar.progress, 1), callback)
            } else {
                map.animateCamera(update, callback)
            }
        } else {
            map.moveCamera(update)
        }
    }

    override fun onCameraMoveStarted(reason: Int) {
        if (!isCanceled) map.clear()

        var reasonText = "UNKNOWN_REASON"
        currPolylineOptions = PolylineOptions().width(5f)
        when (reason) {
            OnCameraMoveStartedListener.REASON_GESTURE -> {
                currPolylineOptions?.color(Color.BLUE)
                reasonText = "GESTURE"
            }
            OnCameraMoveStartedListener.REASON_API_ANIMATION -> {
                currPolylineOptions?.color(Color.RED)
                reasonText = "API_ANIMATION"
            }
            OnCameraMoveStartedListener.REASON_DEVELOPER_ANIMATION -> {
                currPolylineOptions?.color(Color.GREEN)
                reasonText = "DEVELOPER_ANIMATION"
            }
        }
        Log.d(TAG, "onCameraMoveStarted($reasonText)")
        addCameraTargetToPath()
    }

    /**
     * Ensures that currPolyLine options is not null before accessing it
     *
     * @param stuffToDo the code to be executed if currPolylineOptions is not null
     */
    private fun checkPolylineThen(stuffToDo: () -> Unit) {
        if (currPolylineOptions != null) stuffToDo()
    }

    override fun onCameraMove() {
        Log.d(TAG, "onCameraMove")
        // When the camera is moving, add its target to the current path we'll draw on the map.
        checkPolylineThen { addCameraTargetToPath() }
    }

    override fun onCameraMoveCanceled() {
        // When the camera stops moving, add its target to the current path, and draw it on the map.
        checkPolylineThen {
            addCameraTargetToPath()
            map.addPolyline(currPolylineOptions!!)
        }

        isCanceled = true  // Set to clear the map when dragging starts again.
        currPolylineOptions = null
        Log.d(TAG, "onCameraMoveCancelled")
    }

    override fun onCameraIdle() {
        checkPolylineThen {
            addCameraTargetToPath()
            map.addPolyline(currPolylineOptions!!)
        }

        currPolylineOptions = null
        isCanceled = false  // Set to *not* clear the map when dragging starts again.
        Log.d(TAG, "onCameraIdle")
    }
    private fun addCameraTargetToPath() {
        currPolylineOptions?.add(map.cameraPosition.target)
    }
}

Java

// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


package com.example.mapdemo;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.CancelableCallback;
import com.google.android.gms.maps.GoogleMap.OnCameraIdleListener;
import com.google.android.gms.maps.GoogleMap.OnCameraMoveCanceledListener;
import com.google.android.gms.maps.GoogleMap.OnCameraMoveListener;
import com.google.android.gms.maps.GoogleMap.OnCameraMoveStartedListener;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;

/**
 * This shows how to change the camera position for the map.
 */
public class CameraDemoActivity extends AppCompatActivity implements
        OnCameraMoveStartedListener,
        OnCameraMoveListener,
        OnCameraMoveCanceledListener,
        OnCameraIdleListener,
        OnMapReadyCallback {
    private static final String TAG = CameraDemoActivity.class.getName();

    /**
     * The amount by which to scroll the camera. Note that this amount is in raw pixels, not dp
     * (density-independent pixels).
     */
    private static final int SCROLL_BY_PX = 100;

    public static final CameraPosition BONDI =
            new CameraPosition.Builder().target(new LatLng(-33.891614, 151.276417))
                    .zoom(15.5f)
                    .bearing(300)
                    .tilt(50)
                    .build();

    public static final CameraPosition SYDNEY =
            new CameraPosition.Builder().target(new LatLng(-33.87365, 151.20689))
                    .zoom(15.5f)
                    .bearing(0)
                    .tilt(25)
                    .build();

    private GoogleMap map;
    private CompoundButton animateToggle;
    private CompoundButton customDurationToggle;
    private SeekBar customDurationBar;
    private PolylineOptions currPolylineOptions;
    private boolean isCanceled = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_demo);
        animateToggle = findViewById(R.id.animate);
        customDurationToggle = findViewById(R.id.duration_toggle);
        customDurationBar = findViewById(R.id.duration_bar);

        updateEnabledState();

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

    @Override
    protected void onResume() {
        super.onResume();
        updateEnabledState();
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;

        map.setOnCameraIdleListener(this);
        map.setOnCameraMoveStartedListener(this);
        map.setOnCameraMoveListener(this);
        map.setOnCameraMoveCanceledListener(this);
        // We will provide our own zoom controls.
        map.getUiSettings().setZoomControlsEnabled(false);
        map.getUiSettings().setMyLocationButtonEnabled(true);

        // Show Sydney
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-33.87365, 151.20689), 10));
    }

    /**
     * When the map is not ready the CameraUpdateFactory cannot be used. This should be called on
     * all entry points that call methods on the Google Maps API.
     */
    private boolean checkReady() {
        if (map == null) {
            Toast.makeText(this, R.string.map_not_ready, Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    /**
     * Called when the Go To Bondi button is clicked.
     */
    public void onGoToBondi(View view) {
        if (!checkReady()) {
            return;
        }

        changeCamera(CameraUpdateFactory.newCameraPosition(BONDI));
    }

    /**
     * Called when the Animate To Sydney button is clicked.
     */
    public void onGoToSydney(View view) {
        if (!checkReady()) {
            return;
        }

        changeCamera(CameraUpdateFactory.newCameraPosition(SYDNEY), new CancelableCallback() {
            @Override
            public void onFinish() {
                Toast.makeText(getBaseContext(), "Animation to Sydney complete", Toast.LENGTH_SHORT)
                        .show();
            }

            @Override
            public void onCancel() {
                Toast.makeText(getBaseContext(), "Animation to Sydney canceled", Toast.LENGTH_SHORT)
                        .show();
            }
        });
    }

    /**
     * Called when the stop button is clicked.
     */
    public void onStopAnimation(View view) {
        if (!checkReady()) {
            return;
        }

        map.stopAnimation();
    }

    /**
     * Called when the zoom in button (the one with the +) is clicked.
     */
    public void onZoomIn(View view) {
        if (!checkReady()) {
            return;
        }

        changeCamera(CameraUpdateFactory.zoomIn());
    }

    /**
     * Called when the zoom out button (the one with the -) is clicked.
     */
    public void onZoomOut(View view) {
        if (!checkReady()) {
            return;
        }

        changeCamera(CameraUpdateFactory.zoomOut());
    }

    /**
     * Called when the tilt more button (the one with the /) is clicked.
     */
    public void onTiltMore(View view) {
        if (!checkReady()) {
            return;
        }

        CameraPosition currentCameraPosition = map.getCameraPosition();
        float currentTilt = currentCameraPosition.tilt;
        float newTilt = currentTilt + 10;

        newTilt = (newTilt > 90) ? 90 : newTilt;

        CameraPosition cameraPosition = new CameraPosition.Builder(currentCameraPosition)
                .tilt(newTilt).build();

        changeCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }

    /**
     * Called when the tilt less button (the one with the \) is clicked.
     */
    public void onTiltLess(View view) {
        if (!checkReady()) {
            return;
        }

        CameraPosition currentCameraPosition = map.getCameraPosition();

        float currentTilt = currentCameraPosition.tilt;

        float newTilt = currentTilt - 10;
        newTilt = (newTilt > 0) ? newTilt : 0;

        CameraPosition cameraPosition = new CameraPosition.Builder(currentCameraPosition)
                .tilt(newTilt).build();

        changeCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }

    /**
     * Called when the left arrow button is clicked. This causes the camera to move to the left
     */
    public void onScrollLeft(View view) {
        if (!checkReady()) {
            return;
        }

        changeCamera(CameraUpdateFactory.scrollBy(-SCROLL_BY_PX, 0));
    }

    /**
     * Called when the right arrow button is clicked. This causes the camera to move to the right.
     */
    public void onScrollRight(View view) {
        if (!checkReady()) {
            return;
        }

        changeCamera(CameraUpdateFactory.scrollBy(SCROLL_BY_PX, 0));
    }

    /**
     * Called when the up arrow button is clicked. The causes the camera to move up.
     */
    public void onScrollUp(View view) {
        if (!checkReady()) {
            return;
        }

        changeCamera(CameraUpdateFactory.scrollBy(0, -SCROLL_BY_PX));
    }

    /**
     * Called when the down arrow button is clicked. This causes the camera to move down.
     */
    public void onScrollDown(View view) {
        if (!checkReady()) {
            return;
        }

        changeCamera(CameraUpdateFactory.scrollBy(0, SCROLL_BY_PX));
    }

    /**
     * Called when the animate button is toggled
     */
    public void onToggleAnimate(View view) {
        updateEnabledState();
    }

    /**
     * Called when the custom duration checkbox is toggled
     */
    public void onToggleCustomDuration(View view) {
        updateEnabledState();
    }

    /**
     * Update the enabled state of the custom duration controls.
     */
    private void updateEnabledState() {
        customDurationToggle.setEnabled(animateToggle.isChecked());
        customDurationBar
                .setEnabled(animateToggle.isChecked() && customDurationToggle.isChecked());
    }

    private void changeCamera(CameraUpdate update) {
        changeCamera(update, null);
    }

    /**
     * Change the camera position by moving or animating the camera depending on the state of the
     * animate toggle button.
     */
    private void changeCamera(CameraUpdate update, CancelableCallback callback) {
        if (animateToggle.isChecked()) {
            if (customDurationToggle.isChecked()) {
                int duration = customDurationBar.getProgress();
                // The duration must be strictly positive so we make it at least 1.
                map.animateCamera(update, Math.max(duration, 1), callback);
            } else {
                map.animateCamera(update, callback);
            }
        } else {
            map.moveCamera(update);
        }
    }

    @Override
    public void onCameraMoveStarted(int reason) {
        if (!isCanceled) {
            map.clear();
        }

        String reasonText = "UNKNOWN_REASON";
        currPolylineOptions = new PolylineOptions().width(5);
        switch (reason) {
            case OnCameraMoveStartedListener.REASON_GESTURE:
                currPolylineOptions.color(Color.BLUE);
                reasonText = "GESTURE";
                break;
            case OnCameraMoveStartedListener.REASON_API_ANIMATION:
                currPolylineOptions.color(Color.RED);
                reasonText = "API_ANIMATION";
                break;
            case OnCameraMoveStartedListener.REASON_DEVELOPER_ANIMATION:
                currPolylineOptions.color(Color.GREEN);
                reasonText = "DEVELOPER_ANIMATION";
                break;
        }
        Log.d(TAG, "onCameraMoveStarted(" + reasonText + ")");
        addCameraTargetToPath();
    }

    @Override
    public void onCameraMove() {
        // When the camera is moving, add its target to the current path we'll draw on the map.
        if (currPolylineOptions != null) {
            addCameraTargetToPath();
        }
        Log.d(TAG, "onCameraMove");
    }

    @Override
    public void onCameraMoveCanceled() {
        // When the camera stops moving, add its target to the current path, and draw it on the map.
        if (currPolylineOptions != null) {
            addCameraTargetToPath();
            map.addPolyline(currPolylineOptions);
        }
        isCanceled = true;  // Set to clear the map when dragging starts again.
        currPolylineOptions = null;
        Log.d(TAG, "onCameraMoveCancelled");
    }

    @Override
    public void onCameraIdle() {
        if (currPolylineOptions != null) {
            addCameraTargetToPath();
            map.addPolyline(currPolylineOptions);
        }
        currPolylineOptions = null;
        isCanceled = false;  // Set to *not* clear the map when dragging starts again.
        Log.d(TAG, "onCameraIdle");
    }

    private void addCameraTargetToPath() {
        LatLng target = map.getCameraPosition().target;
        currPolylineOptions.add(target);
    }
}

नीचे दिए गए कैमरा लिसनर उपलब्ध हैं:

  • जब कैमरा मूव करना शुरू करता है, तो OnCameraMoveStartedListener के onCameraMoveStarted() कॉलबैक को शुरू कर दिया जाता है. कॉलबैक के तरीके को कैमरे के मोशन के लिए, reason मिलता है. इसकी वजह इनमें से कोई एक हो सकती है:

    • REASON_GESTURE से पता चलता है कि मैप पर उपयोगकर्ता के हाथ के जेस्चर (हाव-भाव) के हिसाब से कैमरा हिल गया. मैप को पैन करना, झुकाना, ज़ूम करने के लिए पिंच करना या मैप को घुमाना.
    • REASON_API_ANIMATION बताता है कि एपीआई ने उपयोगकर्ता की ओर से की जाने वाली कार्रवाई के जवाब में कैमरे की जगह बदली है. जैसे, ज़ूम बटन पर टैप करना, 'मेरी जगह' बटन पर टैप करना या किसी मार्कर पर क्लिक करना.
    • REASON_DEVELOPER_ANIMATION से पता चलता है कि आपके ऐप्लिकेशन ने कैमरा मूवमेंट शुरू कर दिया है.
  • OnCameraMoveListener के onCameraMove() कॉलबैक को कई बार शुरू किया जाता है, जब कैमरा मूव होता है या उपयोगकर्ता टचस्क्रीन से इंटरैक्ट करता है. कॉलबैक को कितनी बार शुरू किया जाता है, इस बारे में जानकारी देने वाली गाइड के तौर पर, यह जानना ज़रूरी है कि एपीआई हर फ़्रेम के लिए एक बार कॉलबैक शुरू करता है. हालांकि, ध्यान दें कि इस कॉलबैक को एसिंक्रोनस रूप से शुरू किया जाता है. इसलिए, स्क्रीन पर दिखने वाले कॉन्टेंट के साथ सिंक नहीं होता है. यह भी ध्यान दें कि एक onCameraMove() कॉलबैक और अगले कॉलबैक के बीच, कैमरे की स्थिति में कोई बदलाव नहीं हो सकता.

  • OnCameraIdleListener का OnCameraIdle() कॉलबैक तब शुरू किया जाता है, जब कैमरे का हिलना बंद हो जाता है और उपयोगकर्ता मैप से इंटरैक्ट करना बंद कर देता है.

  • OnCameraMoveCanceledListener के OnCameraMoveCanceled() कॉलबैक को तब शुरू किया जाता है, जब कैमरे की मौजूदा गतिविधि में कोई रुकावट आ जाती है. OnCameraMoveCanceled() कॉलबैक के तुरंत बाद, onCameraMoveStarted() कॉलबैक को नए reason से शुरू कर दिया जाता है.

    अगर आपके ऐप्लिकेशन में साफ़ तौर पर GoogleMap.stopAnimation() को कॉल किया जाता है, तो OnCameraMoveCanceled() कॉलबैक शुरू हो जाता है, लेकिन onCameraMoveStarted() कॉलबैक को चालू नहीं किया जाता है.

मैप पर लिसनर सेट करने के लिए, सेट-लिसनर मेथड को कॉल करें. उदाहरण के लिए, OnCameraMoveStartedListener से कॉलबैक का अनुरोध करने के लिए, GoogleMap.setOnCameraMoveStartedListener() को कॉल करें.

CameraPosition से आपको कैमरे के टारगेट (अक्षांश/देशांतर), ज़ूम, बियरिंग, और टिल्ट की जानकारी मिल सकती है. इन प्रॉपर्टी के बारे में जानने के लिए, कैमरे की पोज़िशन से जुड़ी गाइड देखें.

कारोबारों और लोकप्रिय जगहों से जुड़े इवेंट

डिफ़ॉल्ट रूप से, बुनियादी मैप पर लोकप्रिय जगहें (लोकप्रियियां) उनके अलग-अलग आइकॉन के साथ दिखती हैं. लोकप्रिय जगहों में पार्क, स्कूल, सरकारी इमारतें, और अन्य कारोबार की लोकप्रिय जगहें शामिल हैं. जैसे, दुकानें, रेस्टोरेंट, और होटल.

लोकप्रिय जगह पर क्लिक इवेंट का जवाब दिया जा सकता है. कारोबारों और अन्य लोकप्रिय जगहों की गाइड देखें.

इनडोर मैप इवेंट

आप किसी इनडोर मैप के सक्रिय स्तर को ढूंढने और उसे कस्टमाइज़ करने के लिए इवेंट का इस्तेमाल कर सकते हैं. किसी नई बिल्डिंग पर फ़ोकस हो या किसी बिल्डिंग में नया लेवल चालू हो, तो लिसनर को सेट करने के लिए, OnIndoorStateChangeListener इंटरफ़ेस का इस्तेमाल करें.

GoogleMap.getFocusedBuilding() पर कॉल करके उस बिल्डिंग को पाएं जो फ़िलहाल फ़ोकस में है. मैप को किसी खास अक्षांश/देशांतर पर फ़ोकस करने से आम तौर पर, इमारत के अक्षांश/देशांतर पर फ़ोकस किया जाएगा. हालांकि, इसकी कोई गारंटी नहीं है.

इसके बाद, IndoorBuilding.getActiveLevelIndex() पर कॉल करके, मौजूदा लेवल की जानकारी देखी जा सकती है.

Kotlin



map.focusedBuilding?.let { building: IndoorBuilding ->
    val activeLevelIndex = building.activeLevelIndex
    val activeLevel = building.levels[activeLevelIndex]
}

      

Java


IndoorBuilding building = map.getFocusedBuilding();
if (building != null) {
    int activeLevelIndex = building.getActiveLevelIndex();
    IndoorLevel activeLevel = building.getLevels().get(activeLevelIndex);
}

      

यह तब काम आता है, जब आपको ऐक्टिव लेवल, जैसे कि मार्कर, ग्राउंड ओवरले, टाइल ओवरले, पॉलीगॉन, पॉलीलाइन, और अन्य आकार के लिए कस्टम मार्कअप दिखाना हो.

सलाह: स्ट्रीट लेवल पर वापस जाने के लिए, IndoorBuilding.getDefaultLevelIndex() से डिफ़ॉल्ट लेवल पाएं और IndoorLevel.activate() की मदद से उसे ऐक्टिव लेवल के तौर पर सेट करें.

मार्कर और जानकारी विंडो इवेंट

मार्कर से जुड़े इवेंट को GoogleMap ऑब्जेक्ट पर सेट करके, मार्कर क्लिक और ड्रैग इवेंट के साथ-साथ मार्कर इवेंट भी सुने और उनका जवाब दिया जा सकता है. मार्कर इवेंट से जुड़ी गाइड देखें.

जानकारी विंडो पर भी इवेंट सुने जा सकते हैं.

आकार और ओवरले इवेंट

पॉलीलाइन, पॉलीगॉन, सर्कल, और ग्राउंड ओवरले पर क्लिक इवेंट को सुना जा सकता है और उनके जवाब दिए जा सकते हैं.

जगह के इवेंट

आपका ऐप 'मेरी जगह' लेयर से जुड़े इन इवेंट का जवाब दे सकता है:

  • अगर उपयोगकर्ता 'मेरी जगह' बटन पर क्लिक करता है, तो आपके ऐप्लिकेशन को GoogleMap.OnMyLocationButtonClickListener से onMyLocationButtonClick() कॉलबैक मिलता है.
  • अगर उपयोगकर्ता 'मेरी जगह' के नीले बिंदु पर क्लिक करता है, तो आपके ऐप्लिकेशन को GoogleMap.OnMyLocationClickListener से onMyLocationClick() कॉलबैक मिलता है.

ज़्यादा जानकारी के लिए, मेरी जगह की लेयर की गाइड देखें.