Есть ли способ получить коллекцию маркеров, которая отображается без кластера, используя Open Street Map - PullRequest
0 голосов
/ 27 апреля 2020

Привет, ребята, я использую открыть карту улиц и мне нужна коллекция маркеров, которые отображаются на моем экране и только маркеры, если я разверну кластер и если он будет отображаться на экране устройства, который также добавит к списку Google map уже предоставляют boun dry и метод сбора вот так.

val markerCollection: MarkerManager.Collection? = mClusterManager?.markerCollection
markerList = ArrayList<Marker>(markerCollection?.markers)

но открытая улица map не предоставляет никакого встроенного метода, поэтому есть ли способ получить это

здесь я делюсь фактическими взглядами на него enter image description here здесь я сделал пользовательский класс для кластеров

class CustomMarkerClusterer(
    ctx: Context,
): MarkerClusterer() {
    private var mMaxClusteringZoomLevel = 17
    private var mRadiusInPixels = 100
    private var mRadiusInMeters:Double = 0.toDouble()
    private val mMarkers: MarkerManager.Collection? = null

    /**
     * If you want to change the default text paint (color, size, font)
     */
    private var textPaint: Paint
    private lateinit var mClonedMarkers:ArrayList<Marker>
    private val context:Context
    private val firstClick:Long = 0
    private val lastClick:Long = 0
    private val count:Int = 0

    /**
     * cluster icon anchor
     */
    var mAnchorU = Marker.ANCHOR_CENTER
    var mAnchorV = Marker.ANCHOR_CENTER
    /**
     * anchor point to draw the number of markers inside the cluster icon
     */
    var mTextAnchorU = Marker.ANCHOR_CENTER
    var mTextAnchorV = Marker.ANCHOR_CENTER
    init{
        textPaint = Paint()
        textPaint.setColor(Color.WHITE)
        textPaint.setTextSize(15 * ctx.getResources().getDisplayMetrics().density)
        textPaint.setFakeBoldText(true)
        textPaint.setTextAlign(Paint.Align.CENTER)
        textPaint.setAntiAlias(true)
        this.context = ctx
        val clusterIconD = ctx.getResources().getDrawable(R.drawable.ic_purple_pin)
        val clusterIcon = (clusterIconD as BitmapDrawable).getBitmap()
        setIcon(clusterIcon)
    }
    /**
     * Set the radius of clustering in pixels. Default is 100px.
     */

    open fun getTextPaint(): Paint? {
        return textPaint
    }

    fun setRadius(radius:Int) {
        mRadiusInPixels = radius
    }
    /**
     * Set max zoom level with clustering. When zoom is higher or equal to this level, clustering is disabled.
     * You can put a high value to disable this feature.
     */
    fun setMaxClusteringZoomLevel(zoom:Int) {
        mMaxClusteringZoomLevel = zoom
    }
    /**
     * Radius-Based clustering algorithm
     */
    override fun clusterer(mapView: MapView):ArrayList<StaticCluster> {
        val clusters = ArrayList<StaticCluster>()

        convertRadiusToMeters(mapView)
        mClonedMarkers = ArrayList<Marker>(mItems) //shallow copy
        while (!mClonedMarkers.isEmpty())
        {
            val m = mClonedMarkers.get(0)
            m.setIcon(context.getResources().getDrawable(R.drawable.ic_purple_pin))
            val cluster = createCluster(m, mapView)
            clusters.add(cluster)
        }
        return clusters
    }
    private fun createCluster(m:Marker, mapView:MapView):StaticCluster {
        val clusterPosition = m.getPosition()
        val cluster = StaticCluster(clusterPosition)
        cluster.add(m)
        mClonedMarkers.remove(m)
        if (mapView.getZoomLevel() > mMaxClusteringZoomLevel)
        {

            //above max level => block clustering:
            return cluster
        }

        val it = mClonedMarkers.iterator()

        while (it.hasNext())
        {
            val neighbour = it.next()
            val distance = clusterPosition.distanceToAsDouble(neighbour.getPosition())
            if (distance <= mRadiusInMeters)
            {
                cluster.add(neighbour)
                it.remove()
            }
        }
        return cluster
    }
    override fun buildClusterMarker(cluster:StaticCluster, mapView:MapView):Marker {
        val marker = Marker(mapView)
        marker.setPosition(cluster.getPosition())
        marker.setInfoWindow(null)
        marker.setAnchor(mAnchorU, mAnchorV)

        var mutableBitmap: Bitmap? = null

        if (cluster.getSize() < 10)
        {
            val icon = Bitmap.createBitmap((context.getResources().getDrawable(R.drawable.cluster_blue) as BitmapDrawable).getBitmap())
            mutableBitmap = icon.copy(Bitmap.Config.ARGB_8888, true)
            val iconCanvas = Canvas(mutableBitmap)
            // iconCanvas.drawBitmap(mClusterIcon, 0, 0, null);
            val text = "" + cluster.getSize()
            textPaint.setColor(Color.WHITE)
            val textHeight = (textPaint.descent() + textPaint.ascent()).toInt()
            iconCanvas.drawText(text,
                mTextAnchorU * icon.getWidth(),
                mTextAnchorV * icon.getHeight() - textHeight / 2,
                textPaint)
            marker.setIcon(BitmapDrawable(mapView.getContext().getResources(), mutableBitmap))

        }
        else if (cluster.getSize() > 10 && cluster.getSize() < 50)
        {
            val icon = Bitmap.createBitmap((context.getResources().getDrawable(R.drawable.cluster_blue) as BitmapDrawable).getBitmap())
            mutableBitmap = icon.copy(Bitmap.Config.ARGB_8888, true)
            val iconCanvas = Canvas(mutableBitmap)
            //iconCanvas.drawBitmap(mClusterIcon, 0, 0, null);
            textPaint.setColor(Color.WHITE)
            val text = "" + cluster.getSize()
            val textHeight = (textPaint.descent() + textPaint.ascent()).toInt()
            iconCanvas.drawText(text,
                mTextAnchorU * icon.getWidth(),
                mTextAnchorV * icon.getHeight() - textHeight / 2,
                textPaint)
            marker.setIcon(BitmapDrawable(mapView.getContext().getResources(), mutableBitmap))
        }
        else if (cluster.getSize() > 50 && cluster.getSize() < 100)
        {
            val icon = Bitmap.createBitmap((context.getResources().getDrawable(R.drawable.cluster_blue) as BitmapDrawable).getBitmap())
            mutableBitmap = icon.copy(Bitmap.Config.ARGB_8888, true)
            val iconCanvas = Canvas(mutableBitmap)
            //iconCanvas.drawBitmap(mClusterIcon, 0, 0, null);
            textPaint.setColor(Color.WHITE)
            val text = "" + cluster.getSize()
            val textHeight = (textPaint.descent() + textPaint.ascent()).toInt()
            iconCanvas.drawText(text,
                mTextAnchorU * icon.getWidth(),
                mTextAnchorV * icon.getHeight() - textHeight / 2,
                textPaint)
            marker.setIcon(BitmapDrawable(mapView.getContext().getResources(), mutableBitmap))
        }
        else if (cluster.getSize() > 100)
        {
            val icon = Bitmap.createBitmap((context.getResources().getDrawable(R.drawable.cluster_blue) as BitmapDrawable).getBitmap())
            mutableBitmap = icon.copy(Bitmap.Config.ARGB_8888, true)
            val iconCanvas = Canvas(mutableBitmap)
            //iconCanvas.drawBitmap(mClusterIcon, 0, 0, null);
            textPaint.setColor(Color.WHITE)
            val text = "" + cluster.getSize()
            val textHeight = (textPaint.descent() + textPaint.ascent()) .toInt()
            iconCanvas.drawText(text,
                mTextAnchorU * icon.getWidth(),
                mTextAnchorV * icon.getHeight() - textHeight / 2,
                textPaint)
            marker.setIcon(BitmapDrawable(mapView.getContext().getResources(), mutableBitmap))
        }
        marker.setOnMarkerClickListener(object: Marker.OnMarkerClickListener {
            override fun onMarkerClick(marker:Marker, mapView:MapView):Boolean {
                mapView.getController().setZoom(mapView.getZoomLevelDouble() + 2)
                InfoWindow.closeAllInfoWindowsOn(mapView);
                return true
            }
        })

        return marker
    }

    override fun renderer(clusters:ArrayList<StaticCluster>, canvas:Canvas, mapView:MapView) {
        for (cluster in clusters)
        {
            if (cluster.getSize() === 1)
            {
                //cluster has only 1 marker => use it as it is:
                cluster.setMarker(cluster.getItem(0))
            }else
            {
                //only draw 1 Marker at Cluster center, displaying number of Markers contained
                val m = buildClusterMarker(cluster, mapView)
                cluster.setMarker(m)

            }

        }

    }
    private fun convertRadiusToMeters(mapView:MapView) {
        val mScreenRect = mapView.getIntrinsicScreenRect(null)
        val screenWidth = mScreenRect.right - mScreenRect.left
        val screenHeight = mScreenRect.bottom - mScreenRect.top
        val bb = mapView.getBoundingBox()
        val diagonalInMeters = bb.getDiagonalLengthInMeters()
        val diagonalInPixels = Math.sqrt((screenWidth * screenWidth + screenHeight * screenHeight).toDouble())
        val metersInPixel = diagonalInMeters / diagonalInPixels
        mRadiusInMeters = mRadiusInPixels * metersInPixel
    }

}

, так как мы можем получить коллекцию маркеров, которые отображаются на экране устройства только без кластера?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...