Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.1 kB
1
Indexable
Never
class Custommarker2(

    private val context: Context,
    private val mapView: MapView?,
    label: String?,
    position: GeoPoint?
) : Marker(mapView) {
    private var labelText: String = label ?: ""
    private val paintText: Paint = Paint()
    private  var geopoint : GeoPoint? = position
    private val paintBackground: Paint = Paint()
    var labelVisible: Boolean = false
    init {
        title = labelText
        setIcon(null) // You can set a custom icon if needed
        position?.let { setPosition(it) }
        paintText.color = android.graphics.Color.WHITE
        paintText.textSize = 20f // Customize label text size
        paintText.textAlign = Paint.Align.CENTER
        paintBackground.color = Color.parseColor("#A6000000")
        setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_TOP)
    }
    // Additional constructor to set the initial label visibility

    constructor(
        context: Context,
        mapView: MapView?,
        label: String?,
        position: GeoPoint?,
        initialLabelVisibility: Boolean
    ) : this(context, mapView, label, position) {
        labelVisible = initialLabelVisibility

    }
    override fun draw(canvas: Canvas?, mapView: MapView?, shadow: Boolean) {
        super.draw(canvas, mapView, shadow)

        if (!shadow  && !labelText.isNullOrBlank()) {
            val customfont = ResourcesCompat.getFont(context,R.font.montserrat_semibold)
            paintText.setTypeface(customfont)
            val positionPixels = mapView?.projection?.toPixels(this.position, null)
            positionPixels?.let {
                val labelx = it.x.toFloat()
                val labely = it.y.toFloat() - paintText.textSize - 12
                val backgroundDrawable =
                    ContextCompat.getDrawable(context, R.drawable.rounded_background)
                val left = (labelx - paintText.measureText(labelText) / 2).toInt() + -10
                val top = (labely - paintText.textSize).toInt()
                val right = (labelx + paintText.measureText(labelText) / 2).toInt() + 5
                val bottom = labely.toInt() + 40
                backgroundDrawable?.bounds = Rect(left, top, right, bottom)
                if (canvas != null) {
                    backgroundDrawable?.draw(canvas)
                }
                canvas?.drawText(labelText, labelx, labely - paintText.ascent(), paintText)
            }
        }else{
//            labelVisible = false
//            Log.d("", "hideLabel:caled00000 ")
        }
    }
    // Method to show the label

    fun showLabel() {
        labelVisible = true
        mapView?.invalidate()
    }
    // Method to hide the label
    fun hideLabel() {
//        Log.d("", "hideLabel:caled ")
        labelVisible = false
        mapView?.invalidate()
    }
    fun updatelabel(txtvalue: String){
        labelText = txtvalue
        title = txtvalue
        mapView?.invalidate()
    }
    fun updategeopoint(newgeopoint : GeoPoint){
        geopoint = newgeopoint
        mapView?.invalidate()
    }

}