【问题标题】:Marker not showing on map in Android标记未在 Android 中的地图上显示
【发布时间】:2021-12-18 21:48:05
【问题描述】:

我刚刚创建了一个 Google 地图片段。标记 sydney 没有显示...我忘了添加什么吗?我已经添加了我的 Google 地图密钥

class fragment_map : Fragment() {

    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 sydney = LatLng(-34.0, 151.0)
        googleMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_map, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
        mapFragment?.getMapAsync(callback)
    }
}

【问题讨论】:

    标签: android android-studio kotlin google-maps key


    【解决方案1】:

    您的 onMapReady 似乎不完整。试试这个吧?

    class fragment_map : Fragment() {
    
        private val callback = OnMapReadyCallback { googleMap ->
            override fun onMapReady(googleMap: GoogleMap) {
                mMap = googleMap
    
                val boundsBuilder = LatLngBounds.Builder()
                val sydney = LatLng(-34.0, 151.0)
                boundsBuilder.include(latlng)
    
                Map.addMarker(MarkerOptions().position(latlng).title("Sydney"))
                mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(boundsBuilder.build(), 1000, 1000, 0))
            }
    
    
        }
    
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return inflater.inflate(R.layout.fragment_map, container, false)
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
            mapFragment?.getMapAsync(callback)
        }
    }
      
    

    【讨论】:

    • 我需要创建一个新函数吗?因为它似乎不起作用。
    • 尝试按照本教程进行操作 -> https://www.youtube.com/watch?v=m_H3JuybsJ0 了解发生了什么。但本质上,您需要在代码中具有该功能。您可以将其复制并粘贴到您的 onCreate 函数上方。我将编辑我的答案,以反映它。
    猜你喜欢
    • 2017-01-09
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 2015-01-25
    • 1970-01-01
    相关资源
    最近更新 更多