Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

android - 使用Kotlin和Android 10的ClusterItem和自定义标记颜色(ClusterItem and custom marker color with Kotlin and Android 10)

I'm trying to set a custom color for a marker in a cluster, based on JSON output.

(我正在尝试根据JSON输出为群集中的标记设置自定义颜色。)

How would I change the marker color based on that serial condition?

(如何根据该序列条件更改标记颜色?)
The way I did previously is commented out.

(我以前的方法已被注释掉。)

Basically the relevant code is this, I'm not including the actual clusterManager setup things:

(基本上相关的代码是这样,我不包括实际的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
    }
}

Test cluster item code is based off the answer I got from a another question here.

(测试集群项目代码基于我从另一个问题获得的答案。)
Found here: Clustering markers on Android 10 with Kotlin

(找到此处: 使用Kotlin在Android 10上聚类标记)

  ask by samip537 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...