При приближение зума карты кластеры (метки) не отображаються на Яндекс Map Kit (android Kotlin)?
Требуеться реализовать корректное отображение меток на карте.
Реализовал отображение меток. Но при приближения зума метки одна за другой пропадают с экрана. Т.е. непонятно на какой объект и куда ссылается метка.
class TextImageProvider(private val text: String, private val context: Context) : ImageProvider() {
private val FONT_SIZE = 15f
private val MARGIN_SIZE = 3f
private val STROKE_SIZE = 3f
override fun getId(): String {
return "text_$text"
}
override fun getImage(): Bitmap {
val metrics = DisplayMetrics()
val manager: WindowManager = getSystemService(context, WindowManager::class.java) as WindowManager
manager.getDefaultDisplay().getMetrics(metrics)
val textPaint = Paint()
textPaint.setTextSize(FONT_SIZE * metrics.density)
textPaint.setTextAlign(Align.CENTER)
textPaint.setStyle(Paint.Style.FILL)
textPaint.setAntiAlias(true)
val widthF: Float = textPaint.measureText(text)
val textMetrics: Paint.FontMetrics = textPaint.getFontMetrics()
val heightF: Float = Math.abs(textMetrics.bottom) + Math.abs(textMetrics.top)
val textRadius =
Math.sqrt((widthF * widthF + heightF * heightF).toDouble()).toFloat() / 2
val internalRadius: Float = textRadius + MARGIN_SIZE * metrics.density
val externalRadius: Float = internalRadius + STROKE_SIZE * metrics.density
val width = (2 * externalRadius + 0.5).toInt()
val bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val backgroundPaint = Paint()
backgroundPaint.setAntiAlias(true)
backgroundPaint.setColor(Color.BLUE)
canvas.drawCircle(width / 2f, width / 2f, externalRadius, backgroundPaint)
backgroundPaint.setColor(Color.WHITE)
canvas.drawCircle(width / 2f, width / 2f, internalRadius, backgroundPaint)
canvas.drawText(
text,
width / 2f,
width / 2 - (textMetrics.ascent + textMetrics.descent) / 2,
textPaint
)
return bitmap
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentPickUpBinding.bind(view)
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(requireActivity())
fragmentBinding = binding
fragmentBinding.apply {
initViews()
initObservable()
mapYandex.map.move(
CameraPosition(TARGET_LOCATION, 17.0f, 0.0f, 0.0f),
Animation(Animation.Type.SMOOTH, 5f),
null
)
val imageProvider: ImageProvider = ImageProvider.fromResource(
context, R.drawable.ic_maps_pin_cluster
)
val collectionCluster: ClusterizedPlacemarkCollection =
mapYandex.map.mapObjects.addClusterizedPlacemarkCollection(this@PickUpFragment)
val places = TempDataGenerator.getPoint()
collectionCluster.addPlacemarks(places, imageProvider, IconStyle()
.setAnchor(PointF(0.5F, 05f))
.setRotationType(RotationType.ROTATE)
.setZIndex(1f)
.setScale(0.5f)
)
collectionCluster.clusterPlacemarks(50.0, 15)
}
}
override fun onClusterAdded(cluster: Cluster) {
cluster.appearance.setIcon(
TextImageProvider(cluster.size.toString(), requireContext())
)
cluster.addClusterTapListener(this)
}
override fun onClusterTap(cluster: Cluster): Boolean {
requireContext().toast("Нажал на кластер")
return true
}
Источник: Stack Overflow на русском