简体   繁体   English

在圆半径内显示 map 标记隐藏在半径迭代器问题之外的标记

[英]Show map markers within Circle radius hide markers outside of radius iterator issue

I'm needing to show markers within my circle radius.我需要在我的圆半径内显示标记。 I'm using Kotlin.我正在使用 Kotlin。 I have the circle radius updating with every onCameraIdleListener.我有每个 onCameraIdleListener 更新的圆半径。 The issue I'm having is that I don't understand how to use the for method correctly.我遇到的问题是我不明白如何正确使用 for 方法。 My end goal is to have the markers hidden when they are outside of the radius of the circle.我的最终目标是在标记位于圆的半径之外时隐藏它们。 so here is the code I have that creates the markers.所以这是我创建标记的代码。 This is where I store the location key/value:这是我存储位置键/值的地方:

private var test = mapOf(
        "TESTLOCATION" to LatLng(34.695779, -110.344185),
)

this is the code I have that adds the marker to the map:这是我将标记添加到 map 的代码:

private fun addTestLocationToMap(){
    val placeDetailsMap = mutableMapOf(

            "TESTLOCATION" to PlaceDetails(
                    position = test.getValue("TESTLOCATION"), title = "Test Location", icon = (BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)), snippet = "Test Location"
            ),

    )
    placeDetailsMap.keys.map{
        with(placeDetailsMap.getValue(it)){
            mMap.addMarker(MarkerOptions()
                    .position(position)
                    .title(title)
                    .snippet(snippet)
                    .icon(icon)
                    .infoWindowAnchor(infoWindowAnchorX, infoWindowAnchorY)
                    .draggable(draggable)
                    .zIndex(zIndex)
                    .visible(isVisible)
            )

        }
    }
}

Here is the Class I'm using for the addTestLocationToMap:这是我用于 addTestLocationToMap 的 Class:

class PlaceDetails(
    val position: LatLng,
    val title: String = "Marker",
    val snippet: String? = null,
    val icon: BitmapDescriptor = BitmapDescriptorFactory.defaultMarker(),
    val infoWindowAnchorX: Float = 0.5F,
    val infoWindowAnchorY: Float = 0F,
    val draggable: Boolean = false,
    val zIndex: Float = 0F,
    val isVisible: Boolean = true,
    val setVisible: Boolean = true)

Here is the Class I have for the Circle:这是我为 Circle 准备的 Class:

class Circle(
    val center: LatLng,
    val radius: Double,
    val strokeColor: Int,
    val fillColor: Int,
    val draggable: Boolean = false)

Here is how I'm adding the circle to the map:这是我将圆圈添加到 map 的方法:

private fun addCircle() {

val newCircle = mutableMapOf(
        "CIRCLE" to Circle(
                center = mMap.cameraPosition.target,
                radius = 241402.0,
                strokeColor = Color.RED,
                fillColor = 0x00000000
        )


)

newCircle.keys.map {
    with(newCircle.getValue(it)) {
        mMap.addCircle(CircleOptions()
                .center(center)
                .radius(radius)
                .strokeColor(strokeColor)
                .fillColor(fillColor)
                    .draggable(draggable)
        )

    }
}

} }

Here are the private var at the top of the code that updates the lat/lng for the camera:以下是代码顶部的私有变量,用于更新相机的 lat/lng:

private var cameraLat: Double = 0.0
private var cameraLng: Double = 0.0

Here is the map of for the Circle:这是 Circle 的 map:

private var circle = mapOf(


        "CIRCLE" to LatLng(cameraLat, cameraLng),)

This is how I'm updating the camera's这就是我更新相机的方式

 mMap.setOnCameraIdleListener {
        var cameraLatUpdate = mMap.cameraPosition.target.latitude
        var cameraLngUpdate = mMap.cameraPosition.target.longitude
        cameraLatLng = mMap.cameraPosition.target
        cameraLat = cameraLatUpdate
        cameraLng = cameraLngUpdate}

This is how I'm trying to show/hide markers in the radius.这就是我试图在半径中显示/隐藏标记的方式。 Here is:这是:

private lateinit var marker: Marker

This is the code for the radius:这是半径的代码:

fun addCircleToMap() {

for (marker: LatLng in circle.getValue("CIRCLE") ){ if (SphericalUtil.computeDistanceBetween(circle.getValue("CIRCLE"), test.getValue("TESTLOCATION") ) < 241402.0){ marker.isVisible = true} for (marker: LatLng in circle.getValue("CIRCLE") ){ if (SphericalUtil.computeDistanceBetween(circle.getValue("CIRCLE"), test.getValue("TESTLOCATION") ) < 241402.0){ marker.isVisible = true}

This is the error I'm getting "For-loop range must have an 'iterator()' method" This is where I'm stuck at.这是我得到的错误“For-loop range must have an 'iterator()' method”这是我遇到的问题。 I have tried to look into iterator but I'm not understanding it.我试图研究迭代器,但我不理解它。 Does my code seem right?我的代码看起来对吗? Am I doing something wrong with the code?我对代码做错了吗? I got the general idea from this comment on another similar question.我从对另一个类似问题的评论中得到了大致的想法。 this code was in java I do believe.我相信这个代码在 java 中。 I've done my best to try and keep it similar.我已尽我所能保持相似。 but I'm not sure on how I can iterator this for loop?但我不确定如何迭代这个 for 循环? Thank you for all your help!谢谢你的帮助!

When you call for statement it should be for an iterable collection, but you are calling one unique value, which is circle.getValue("CIRCLE"), I think you should directly assing the value to the marker当您调用 for 语句时,它应该是一个可迭代的集合,但是您调用的是一个唯一值,即 circle.getValue("CIRCLE"),我认为您应该直接将值分配给标记

val marker = circle.getValue("CIRCLE") 

So with some code changes and I was able to successfully hide the markers outside of the circle radius with this code.因此,通过一些代码更改,我能够使用此代码成功地将标记隐藏在圆半径之外。

    private lateinit var testMarkers:MutableList<Marker>




fun testMarkersForLoops(): MutableList<Marker> {

    val testMarkers = mutableListOf(
            mMap.addMarker(MarkerOptions().position(LatLng(34.695779, -87.634612)).title("testMarker").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)).visible(false))).toMutableList() return testMarkers

// just make sure to have the visible set to false in order to hide them until their in circle radius // 只需确保将可见设置为 false 以便隐藏它们直到它们的圆半径

}

fun loopForMarkers(){有趣的 loopForMarkers(){

    val circle = mMap.addCircle(CircleOptions().center(mMap.cameraPosition.target).radius(241402.0).strokeColor(Color.RED).fillColor(0x00000000))
    for (marker: Marker in testMarkers ){
        if (SphericalUtil.computeDistanceBetween(circle.center, marker.position) < 241402.0){
            marker.isVisible = true
            Log.i(TAG,"Supposed to set Loves markers within the radius to true testing to see if this runs.")
        }
    }

   
}

then adding this within the onMapReady:然后在 onMapReady 中添加:

mMap.setOnCameraIdleListener {
        var cameraLatUpdate = mMap.cameraPosition.target.latitude
        var cameraLngUpdate = mMap.cameraPosition.target.longitude
        cameraLatLng = mMap.cameraPosition.target
        cameraLat = cameraLatUpdate
        cameraLng = cameraLngUpdate
        
        mMap.clear()
        testMarkers = testMarkersForLoops()

        addCircleToMap()
        loopForMarkers()
    }

you have to call the location list function after the.clear() I was having the.clear() in the wrong spot.您必须在.clear() 之后调用位置列表 function 我在错误的位置放置了.clear()。 Instead of creating the mapOf then using the class PlaceDetails to assign each locations info I just did it with the mutableListOf().而不是创建 mapOf 然后使用 class PlaceDetails 来分配每个位置信息,我只是使用 mutableListOf() 完成的。 in the for loop just call the mutableListOf name you assigned for it.在 for 循环中只需调用您为其分配的 mutableListOf 名称。 The radius is in Meters, you can change it to the radius you want.半径以米为单位,您可以将其更改为您想要的半径。 this is roughly 150 mile raidus.这大约是150英里的raidus。

I use the circle.center for the latlng of the center of the circle, having the:我使用 circle.center 作为圆心的纬度,具有:

.center(mMap.cameraPosition.target)

Once you place this in the onCameraIdle, this will update the center LatLng with every camera idle.一旦你把它放在 onCameraIdle 中,这将在每个相机空闲时更新中心 LatLng。

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

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