【问题标题】:Creating dinamicaly Markers and accessing by variables for Google Maps Api on Kotlin在 Kotlin 上为 Google Maps Api 创建动态标记和变量访问
【发布时间】:2020-01-22 22:57:41
【问题描述】:

我正在制作一个项目,我需要在其中添加和删除一些动态创建的标记。我正在用这种方式创建我的标记:

 private fun AddMarkerFromArray(name:String, adrs:String,cod:String,imgSrc:String){

    val info = MarkerDataNormal(name, adrs,
        cod )
    val markerOptions = MarkerOptions()
    markerOptions.position(LatLng(0.0,0.0))
        .title("Location Details")
        .snippet("I am custom Location Marker.")
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)
            )
    val customInfoWindow = CustomInfoWindowGoogleMap(this)
    mMap.setInfoWindowAdapter(customInfoWindow)
    val spot: Marker = mMap.addMarker(markerOptions)
    spot.tag = info
   // spot.showInfoWindow()
}

这是剩下的代码:

   data class MarkerDataNormal(val mLocatioName: String,
                        val mLocationAddres: String,
                        val mLocationCood: String)

   class CustomInfoWindowGoogleMap(val context: Context) : GoogleMap.InfoWindowAdapter {

   override fun getInfoContents(p0: Marker?): View {

    var mInfoView = (context as Activity).layoutInflater.inflate(R.layout.markernormal, null)
    var mInfoWindow: MarkerDataNormal? = p0?.tag as MarkerDataNormal?
    mInfoView.txtNombre.text = mInfoWindow?.mLocatioName
    mInfoView.txtDireccion.text = mInfoWindow?.mLocationAddres
    mInfoView.txtCoordenadas.text = mInfoWindow?.mLocationCood
    return mInfoView
}

override fun getInfoWindow(p0: Marker): View? {
    return null
   }
}

所以我正在使用该功能来制作我需要的所有标记,它们显示正确,但我不能clear()只有一个原因,因为它们中的每一个都是“现货”

如何更改变量的名称或为每个变量分配一个 id 以供以后访问?

【问题讨论】:

    标签: android google-maps kotlin marker


    【解决方案1】:

    我认为您应该将标记对象的引用存储在List<Marker> 中。这样您以后就可以访问这些参考资料。

    【讨论】:

    • '我对此有点困惑,List 像数组一样工作?如果是这样..我可以为每个人分配一个密钥吗?然后someKeyOfLlist.clear() 和那个标记消失了对吗?
    • 您不能将密钥与List 一起使用。要从地图中删除标记,您可以像这样遍历列表:markerList.forEach { marker -> marker.remove() }
    • 我认为这是查看您如何使用标签的更好答案。您总是可以为您的数据使用不同的容器,但可以分配一个键。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 2019-08-11
    相关资源
    最近更新 更多