مناسبت ها

پلتفرم را انتخاب کنید: Android iOS JavaScript

با استفاده از Maps SDK برای Android، می‌توانید به رویدادهای روی نقشه گوش دهید.

نمونه کد

مخزن ApiDemos در GitHub شامل نمونه هایی است که رویدادها و شنوندگان را نشان می دهد:

کاتلین

جاوا

کلیک روی نقشه / رویدادهای کلیک طولانی

اگر می‌خواهید به ضربه زدن کاربر روی نقطه‌ای روی نقشه پاسخ دهید، می‌توانید از OnMapClickListener استفاده کنید که می‌توانید با تماس با GoogleMap.setOnMapClickListener(OnMapClickListener) را روی نقشه تنظیم کنید. هنگامی که کاربر در جایی از نقشه کلیک می کند (ضربه می زند)، یک رویداد onMapClick(LatLng) دریافت خواهید کرد که مکان روی نقشه را که کاربر روی آن کلیک کرده است را نشان می دهد. توجه داشته باشید که اگر به مکان مربوطه روی صفحه نیاز دارید (بر حسب پیکسل)، می توانید یک Projection از نقشه بدست آورید که به شما امکان می دهد بین مختصات طول و عرض جغرافیایی و مختصات پیکسل صفحه را تبدیل کنید.

همچنین می‌توانید با یک OnMapLongClickListener که می‌توانید با تماس با GoogleMap.setOnMapLongClickListener(OnMapLongClickListener) روی نقشه تنظیم کنید، به رویدادهای کلیک طولانی گوش دهید. این شنونده رفتاری مشابه با شنونده کلیک دارد و در مورد رویدادهای کلیک طولانی با یک پاسخ تماس onMapLongClick(LatLng) مطلع می شود.

غیرفعال کردن رویدادهای کلیک در حالت ساده

برای غیرفعال کردن رویدادهای کلیک روی نقشه در حالت ساده ، setClickable() را در نمای حاوی MapView یا MapFragment فراخوانی کنید. برای مثال، هنگام نمایش یک نقشه یا نقشه ها در یک نمای فهرست، جایی که می خواهید رویداد کلیک کنشی غیرمرتبط با نقشه را فراخوانی کند، مفید است.

گزینه غیرفعال کردن رویدادهای کلیک فقط در حالت ساده موجود است. غیرفعال کردن رویدادهای کلیک، نشانگرها را غیرقابل کلیک می‌کند. بر دیگر کنترل‌های روی نقشه تأثیری نخواهد داشت.

برای MapView :

کاتلین



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

      

جاوا


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

      

برای یک MapFragment :

کاتلین



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

      

جاوا


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

      

رویدادهای تغییر دوربین

نمای نقشه به صورت دوربینی که از پایین در یک صفحه صاف نگاه می کند، مدل سازی شده است. می‌توانید ویژگی‌های دوربین را تغییر دهید تا روی سطح زوم، درگاه مشاهده و پرسپکتیو نقشه تأثیر بگذارد. راهنمای دوربین را ببینید. کاربران همچنین می توانند با انجام حرکات بر روی دوربین تأثیر بگذارند.

با استفاده از شنونده های تغییر دوربین، می توانید حرکات دوربین را پیگیری کنید. برنامه شما می‌تواند اعلان‌هایی را برای رویدادهای شروع، در حال انجام و پایان حرکت دوربین دریافت کند. همچنین می‌توانید ببینید که چرا دوربین حرکت می‌کند، خواه این حرکت ناشی از حرکات کاربر، انیمیشن‌های API داخلی یا حرکات کنترل‌شده توسط توسعه‌دهنده باشد.

نمونه زیر تمام شنوندگان رویداد دوربین موجود را نشان می دهد:

کاتلین

/*
 * 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)
    }
}

جاوا

// 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);
    }
}

شنوندگان دوربین زیر در دسترس هستند:

  • هنگامی که دوربین شروع به حرکت می کند، پاسخ تماس onCameraMoveStarted() OnCameraMoveStartedListener فراخوانی می شود. روش برگشت به تماس reason برای حرکت دوربین دریافت می کند. دلیل آن می تواند یکی از موارد زیر باشد:

    • REASON_GESTURE نشان می‌دهد که دوربین در پاسخ به اشاره کاربر روی نقشه حرکت کرده است، مانند حرکت به سمت چپ، کج کردن، نیشگون گرفتن برای بزرگ‌نمایی، یا چرخش نقشه.
    • REASON_API_ANIMATION نشان می‌دهد که API دوربین را در پاسخ به یک عملکرد کاربر بدون اشاره حرکت داده است، مانند ضربه زدن روی دکمه بزرگ‌نمایی، ضربه زدن روی دکمه موقعیت مکانی من یا کلیک کردن روی یک نشانگر.
    • REASON_DEVELOPER_ANIMATION نشان می دهد که برنامه شما حرکت دوربین را آغاز کرده است.
  • پاسخ تماس onCameraMove() OnCameraMoveListener چندین بار در حالی که دوربین در حال حرکت است یا کاربر در حال تعامل با صفحه لمسی است فراخوانی می شود. به عنوان راهنمایی برای اینکه چند وقت یکبار فراخوانی مجدد فراخوانی می شود، مفید است بدانید که API یک بار در هر فریم تماس را فراخوانی می کند. البته توجه داشته باشید که این تماس به صورت ناهمزمان فراخوانی می شود و بنابراین با آنچه روی صفحه قابل مشاهده است هماهنگ نیست. همچنین توجه داشته باشید که این امکان وجود دارد که موقعیت دوربین بین یک پاسخ onCameraMove() و دیگری بدون تغییر باقی بماند.

  • پاسخ تماس OnCameraIdle() OnCameraIdleListener زمانی فراخوانی می شود که دوربین از حرکت باز می ایستد و کاربر تعامل با نقشه را متوقف می کند.

  • پاسخ تماس OnCameraMoveCanceled() OnCameraMoveCanceledListener زمانی فراخوانی می شود که حرکت فعلی دوربین قطع شده باشد. بلافاصله پس از فراخوانی OnCameraMoveCanceled() ، فراخوانی onCameraMoveStarted() با reason جدید فراخوانی می شود.

    اگر برنامه شما صراحتاً GoogleMap.stopAnimation() را فراخوانی می کند، پاسخ تماس OnCameraMoveCanceled() فراخوانی می شود، اما پاسخ تماس onCameraMoveStarted() فراخوانی نمی شود.

برای تنظیم شنونده روی نقشه، روش set-lister مربوطه را فراخوانی کنید. برای مثال، برای درخواست پاسخ به تماس از OnCameraMoveStartedListener ، با GoogleMap.setOnCameraMoveStartedListener() تماس بگیرید.

می‌توانید هدف (طول/طول جغرافیایی)، زوم، یاتاقان و شیب دوربین را از CameraPosition دریافت کنید. برای جزئیات بیشتر در مورد این ویژگی ها، راهنمای موقعیت دوربین را ببینید.

رویدادهای مربوط به مشاغل و سایر نقاط مورد علاقه

به طور پیش فرض، نقاط مورد علاقه (POI) به همراه نمادهای مربوطه روی نقشه پایه ظاهر می شوند. POI ها شامل پارک ها، مدارس، ساختمان های دولتی و موارد دیگر و همچنین POI های تجاری مانند فروشگاه ها، رستوران ها و هتل ها می شود.

می توانید به رویدادهای کلیک در یک POI پاسخ دهید. راهنمای کسب و کارها و سایر نقاط مورد علاقه را ببینید.

رویدادهای نقشه داخلی

می‌توانید از رویدادها برای پیدا کردن و سفارشی کردن سطح فعال یک نقشه داخلی استفاده کنید. از رابط OnIndoorStateChangeListener برای تنظیم یک شنونده استفاده کنید تا زمانی که ساختمان جدیدی متمرکز شده است یا سطح جدیدی در یک ساختمان فعال می شود، فراخوانی شود.

با فراخوانی GoogleMap.getFocusedBuilding() ساختمانی را که در حال حاضر مورد توجه است، دریافت کنید. قرار دادن نقشه بر روی یک lat/long خاص، به طور کلی ساختمان را در آن lat/long قرار می دهد، اما این تضمین نمی شود.

سپس می توانید سطح فعال فعلی را با فراخوانی IndoorBuilding.getActiveLevelIndex() بیابید.

کاتلین



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

      

جاوا


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

      

اگر می‌خواهید نشانه‌گذاری سفارشی را برای سطح فعال نشان دهید، مانند نشانگرها ، روکش‌های زمین ، پوشش‌های کاشی ، چند ضلعی، چند خطی و سایر اشکال مفید است.

نکته: برای بازگشت به سطح خیابان، سطح پیش‌فرض را از طریق IndoorBuilding.getDefaultLevelIndex() دریافت کنید و از طریق IndoorLevel.activate() آن را به عنوان سطح فعال تنظیم کنید.

نشانگر و رویدادهای پنجره اطلاعات

می‌توانید با تنظیم شنونده مربوطه روی شی GoogleMap که نشانگر به آن تعلق دارد، به رویدادهای نشانگر، از جمله رویدادهای کلیک و کشیدن نشانگر، گوش دهید و به آنها پاسخ دهید. راهنمای رویدادهای نشانگر را ببینید.

همچنین می‌توانید به رویدادها در پنجره‌های اطلاعات گوش دهید.

شکل دادن و روی هم گذاشتن رویدادها

می توانید به رویدادهای کلیک روی چند خط ، چند ضلعی ، دایره و همپوشانی زمین گوش دهید و به آنها پاسخ دهید.

رویدادهای مکان

برنامه شما می تواند به رویدادهای زیر مربوط به لایه My Location پاسخ دهد:

  • اگر کاربر روی دکمه موقعیت مکانی من کلیک کند، برنامه شما یک پاسخ تماس onMyLocationButtonClick() از GoogleMap.OnMyLocationButtonClickListener دریافت می کند.
  • اگر کاربر روی نقطه آبی My Location کلیک کند، برنامه شما یک پاسخ تماس onMyLocationClick() از GoogleMap.OnMyLocationClickListener دریافت می کند.

برای جزئیات، به راهنمای لایه My Location مراجعه کنید.