我希望我的微调框索引来确定地图的缩放

问题描述 投票:0回答:1

我正在制作一个应用程序,显示用户附近的道路施工,我已经实现了一张地图和一个带有三个选项的微调框,我希望我的微调框索引来确定地图的缩放程度。当我编译代码时,它将放大到默认缩放设置,但是当我从下拉列表中选择另一个选项时,什么也没有发生。

这是我的地图回调

     ` private val callback = OnMapReadyCallback { googleMap ->
            /**
             * Manipulates the map once available.
             * This callback is triggered when the map is ready to be used.
             * This is where we can add markers or lines, add listeners or move the camera.
             * In this case, we just add a marker near Sydney, Australia.
             * If Google Play services is not installed on the device, the user will be prompted to
             * install it inside the SupportMapFragment. This method will only be triggered once the
             * user has installed Google Play services and returned to the app.
             */
    
            val (latitude, longitude) = getLocation()
            currentLocation = LatLng(latitude, longitude)
            googleMap.addMarker(MarkerOptions().position(currentLocation).title("Current location"))
            
            //Moves the camera to  users position with the zoom level
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, selectedZoomLevel))
        }` 
    

这是我在项目选定侦听器上的微调器

    binding.searchSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {

                //gets the array of items for the spinner
                val spinnerItems = resources.getStringArray(R.array.spinner_items)

                // Zoom levels based off of my spinner items
                val zoomLevels = mapOf(
                    "10 km" to 12.0f,
                    "25 km" to 8.0f,
                    "50 km" to 2.0f
                )

                // Retrieve selected item and make it a string
                val selectedItem = binding.searchSpinner.selectedItem.toString()

                // Sets the zoom level based on selected item, otherwise default to 12.0f
                selectedZoomLevel = zoomLevels[selectedItem] ?: 12.0f

                // Call showToastWithIndex with the selected position
                showToastWithIndex(requireContext(), spinnerItems, position)


            }
kotlin google-maps location google-geolocation google-maps-android-api-3
1个回答
0
投票

您需要在
moveCamera()
中调用
animateCamera()
onItemSelectedListener
方法来更改地图缩放

首先,您需要确保使用

lateinit
:

为 Google 地图实例创建一个全局变量
private lateinit var gMap: GoogleMap

然后在地图中插入

GoogleMap
实例:

override fun onMapReady(googleMap: GoogleMap) {
    gMap = googleMap
    //..other code..//
}

还有其他方法可以做到这一点,例如将

googleMap
作为参数传递。但最终,这将帮助您稍后在
onItemSelectedListener
中访问它。

因此,完成此操作后,您可以在

moveCamera()
内添加
animateCamera()
onItemSelectedListener
,一切都会正常工作。

您提供的

onItemSelectedListener
代码应如下所示:

binding.searchSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
    override fun onItemSelected(
        parent: AdapterView<*>?,
        view: View?,
        position: Int,
        id: Long
    ) {
    
    //gets the array of items for the spinner
    val spinnerItems = resources.getStringArray(R.array.spinner_items)
    
    // Zoom levels based off of my spinner items
    val zoomLevels = mapOf(
        "10 km" to 12.0f,
        "25 km" to 8.0f,
        "50 km" to 2.0f
    )
    
    // Retrieve selected item and make it a string
    val selectedItem = binding.searchSpinner.selectedItem.toString()
    
    // Sets the zoom level based on selected item, otherwise default to 12.0f
    selectedZoomLevel = zoomLevels[selectedItem] ?: 12.0f
    
    // Call showToastWithIndex with the selected position
    showToastWithIndex(requireContext(), spinnerItems, position)

    // Since you can access googleMaps here, you can call the animateCamera / moveCamera method here:
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, selectedZoomLevel))
    // or...
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, selectedZoomLevel))    
}

这是我作为概念证明而制作的示例代码

MapsActivity.kt

package com.example.kotlindemos

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.Toast

import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import com.example.kotlindemos.databinding.ActivityMapsBinding

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

    val SYDNEY = LatLng(-33.862, 151.21)
    val ZOOM_LEVEL = 13f
    private lateinit var gMap: GoogleMap
    private lateinit var spinner: Spinner

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_basic_map_demo)
        val mapFragment : SupportMapFragment? =
            supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
        mapFragment?.getMapAsync(this)

        spinner = findViewById(R.id.spinner)

        val listItems = listOf("10 km", "25 km", "50 km")

        val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, listItems)
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spinner.adapter = arrayAdapter

        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {

                val selectedItem = parent.getItemAtPosition(position).toString()

                // Zoom levels based off of my spinner items
                val zoomLevels = mapOf(
                    "10 km" to 12.0f,
                    "25 km" to 8.0f,
                    "50 km" to 2.0f
                )

                // Sets the zoom level based on selected item, otherwise default to 12.0f
                val selectedZoomLevel = zoomLevels[selectedItem] ?: 12.0f

                Toast.makeText(applicationContext, "Selected: $selectedZoomLevel", Toast.LENGTH_SHORT).show()

                // Since we have confirmed that the zoom level values are correct, we can now use them to set the zoom level
                gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, selectedZoomLevel))
            }

            override fun onNothingSelected(parent: AdapterView<*>) {
                // write code to perform some action
            }
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just move the camera to Sydney and add a marker in Sydney.
     */
    override fun onMapReady(googleMap: GoogleMap) {
        gMap = googleMap
        with(gMap) {
            moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, ZOOM_LEVEL))
            addMarker(MarkerOptions().position(SYDNEY))
        }
    }
}

layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.kotlindemos.BasicMapDemoActivity"
    android:orientation="vertical">
<!--    insert spinner-->
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        />
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.kotlindemos.BasicMapDemoActivity">

    </androidx.fragment.app.FragmentContainerView>

</LinearLayout>
© www.soinside.com 2019 - 2024. All rights reserved.