【问题标题】:Google maps sdk android markers click listener not working谷歌地图sdk android标记点击监听器不起作用
【发布时间】:2019-09-23 08:24:46
【问题描述】:

我正在尝试使用自定义图标和颜色放置标记。目前在 kotlin for android 工作。我正在尝试按照 google 文档中的说明在点击侦听器上设置标记。但是点击时什么都没有发生。

重写 onMarkerClick 函数并尝试获取该标记的标记,因为我存储在该标记中,在此之前我尝试使用 .equal 方法来测试标记是否等于该事件标记

override fun onMarkerClick(marker: Marker): Boolean {

        // Retrieve the data from the marker.
        var clickCount = marker.tag as Int?

        // Check if a click count was set, then display the click count.
        if (clickCount != null) {
            clickCount = clickCount!! + 1
            marker.tag = clickCount
            Toast.makeText(
                this,
                marker.title +
                        " has been clicked " + clickCount + " times.",
                Toast.LENGTH_SHORT
            ).show()
        }

        // Return false to indicate that we have not consumed the event and that we wish
        // for the default behavior to occur (which is for the camera to move such that the
        // marker is centered and for the marker's info window to open, if it has one).
        return false
    }


    mMap = googleMap
            val streetRight = mMap.addMarker(
                MarkerOptions().position(
                    LatLng(
                        -xx.xxxxx,
                        xxx.xxxxxx
                    )
                ).icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_street_view_person))
            )
            val streeTop = mMap.addMarker(
                MarkerOptions().position(
                    LatLng(
                        -xx.xxxxx,
                        xxx.xxxxxx
                    )
                ).icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_street_view_person))
            )
    streeTop.tag = 1
            streetRight.tag = 2

我希望当我单击标记时,它应该显示单击标记的祝酒词。但是什么都没有发生,只是将那个标记映射到中心。

【问题讨论】:

  • 调试你的代码并在这一行放一个断点`var clickCount = marker.tag as Int?` 如果这行有反应,那么clickCount是空的。
  • @MahmoudShahoud 是的,它正在反应

标签: android google-maps kotlin google-maps-markers kotlin-android-extensions


【解决方案1】:

我能够让您的代码正常工作,但我将侦听器添加到 onMapReady:

   override fun onMapReady(gMap: GoogleMap?) {
        gMap?.let {
             it.setOnMarkerClickListener {
                var clickCount = it.tag as Int?

                // Check if a click count was set, then display the click count.
                if (clickCount != null) {
                    clickCount = clickCount!! + 1
                    it.tag = clickCount
                    Toast.makeText(
                        this@MapFragment.context,
                        it.title +
                                " has been clicked " + clickCount + " times.",
                        Toast.LENGTH_SHORT
                    ).show()
                }

                // Return false to indicate that we have not consumed the event and that we wish
                // for the default behavior to occur (which is for the camera to move such that the
                // marker is centered and for the marker's info window to open, if it has one).
                false
            }
        }
    }

在您调用 .getMapAsync() 后会调用它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 2014-05-10
    • 2019-02-09
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多