繁体   English   中英

使用Kotlin和Android 10的ClusterItem和自定义标记颜色

[英]ClusterItem and custom marker color with Kotlin and Android 10

我正在尝试基于JSON输出为群集中的标记设置自定义颜色。

如何根据该序列条件更改标记颜色?
我以前的方法已被注释掉。

基本上相关的代码是这样,我不包括实际的clusterManager设置内容:

private fun addItems() {
        // Instantiate the RequestQueue
        val queue = Volley.newRequestQueue(this)
        val builder = StringBuilder()
        val url = builder
            .append("https://")
            .append(R.string.api_base_url)
            .append("vehicle/status/ready?")
            .append("lat=<censored>" + "&lng=<censored>").toString()
        Log.d("JSON_URL", url)
        // Create request and listeners
        val jsonArrayRequest = JsonArrayRequest(
            Request.Method.GET, url, null,
            Response.Listener { response ->
                for(i in 0 until response.length()) {
                    val item: JSONObject = response.getJSONObject(i)
                    val itemLocation = item.getJSONArray("location")
                    val color = giveMarkerColor(item)
                    val point = LatLng(itemLocation.getDouble(0), itemLocation.getDouble(1))
                    //markers[i] = mMap.addMarker(
                    //    MarkerOptions()
                    //        .position(point)
                    //        .icon(BitmapDescriptorFactory.defaultMarker(color))
                    //        .title(item["short"]
                    //            .toString())
                    //        .snippet(giveSnippet(item))
                    val actualItem = giveSnippet(item)?.let {
                        TestItem(itemLocation.getDouble(0),
                            itemLocation.getDouble(1), item.getString("short"), it
                        )
                    }
                    mClusterManager.addItem(actualItem)
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(point))
                }
            },
            Response.ErrorListener { error ->
                Log.d("JSON_ERROR",error.toString())
            }
        )
        // Add the request to the RequestQueue.
        queue.add(jsonArrayRequest)
    }

private fun giveMarkerColor(item: JSONObject): Float {
        return when(item.getString("serial")) {
            "null" -> BitmapDescriptorFactory.HUE_BLUE
            else -> BitmapDescriptorFactory.HUE_RED
        }
    }

class TestItem: ClusterItem {
    private val mPosition: LatLng
    private val mTitle: String
    private val mSnippet: String

    constructor(lat: Double, lng: Double) {
        mPosition = LatLng(lat, lng)
        mTitle = ""
        mSnippet = ""
    }

    constructor(lat: Double, lng: Double, title: String, snippet: String) {
        mPosition = LatLng(lat, lng)
        mTitle = title
        mSnippet = snippet
    }

    override fun getPosition(): LatLng {
        return mPosition
    }

    override fun getTitle(): String {
        return mTitle
    }

    override fun getSnippet(): String {
        return mSnippet
    }
}

测试集群项目代码基于我从另一个问题获得的答案。
找到此处: 使用Kotlin在Android 10上聚类标记

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM