Google maps 显示圆半径范围内的地图标记隐藏半径范围外的标记迭代器问题

Google maps 显示圆半径范围内的地图标记隐藏半径范围外的标记迭代器问题,google-maps,for-loop,kotlin,iterator,android-maps-utils,Google Maps,For Loop,Kotlin,Iterator,Android Maps Utils,我需要显示圆圈半径内的标记。我在用科特林。我用每个OnCameraideListener更新了圆半径。我遇到的问题是,我不知道如何正确使用for方法。我的最终目标是将标记隐藏在圆半径之外。这是我创建标记的代码。这是我存储位置键/值的位置: private var test = mapOf( "TESTLOCATION" to LatLng(34.695779, -110.344185), ) 这是我为地图添加标记的代码: private fun addTes

我需要显示圆圈半径内的标记。我在用科特林。我用每个OnCameraideListener更新了圆半径。我遇到的问题是,我不知道如何正确使用for方法。我的最终目标是将标记隐藏在圆半径之外。这是我创建标记的代码。这是我存储位置键/值的位置:

private var test = mapOf(
        "TESTLOCATION" to LatLng(34.695779, -110.344185),
)
这是我为地图添加标记的代码:

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)
            )

        }
    }
}
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)
        )

    }
}
下面是我用于addTestLocationToMap的类:

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)
以下是我为圆圈所上的课程:

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


        "CIRCLE" to LatLng(cameraLat, cameraLng),)
下面是我将圆添加到地图的方式:

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)
            )

        }
    }
}
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)
        )

    }
}
}

以下是更新摄像机lat/lng代码顶部的专用var:

private var cameraLat: Double = 0.0
private var cameraLng: Double = 0.0
这是圆圈的地图:

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


        "CIRCLE" to LatLng(cameraLat, cameraLng),)
这就是我更新相机的方法

 mMap.setOnCameraIdleListener {
        var cameraLatUpdate = mMap.cameraPosition.target.latitude
        var cameraLngUpdate = mMap.cameraPosition.target.longitude
        cameraLatLng = mMap.cameraPosition.target
        cameraLat = cameraLatUpdate
        cameraLng = cameraLngUpdate}
这就是我试图显示/隐藏半径中的标记的方式。这是:

private lateinit var marker: Marker
这是半径的代码:

fun addCircleToMap() {
用于(标记:圈中的LatLng.getValue(“圈”)){ if(SphereCalutil.ComputedDistanceBeween(circle.getValue(“circle”)、test.getValue(“TESTLOCATION”)<241402.0){ marker.isVisible=true}


这是我得到的错误“For循环范围必须有一个‘iterator()’方法”这就是我所要解决的问题。我曾尝试研究迭代器,但我不理解它。我的代码似乎正确吗?我的代码是否有问题?我从另一个类似的问题上得到了大致的想法。我相信这段代码是用java编写的。我已经尽了最大的努力来保持它的相似性。但我不确定如何对其进行迭代器for loop?感谢您的帮助!

当您调用for语句时,它应该是一个iterable集合,但您调用的是一个唯一的值,即circle.getValue(“circle”),我认为您应该直接将该值分配给标记

val marker = circle.getValue("CIRCLE") 

因此,通过一些代码更改,我能够成功地用这段代码将标记隐藏在圆半径之外

    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
有趣的恶作剧(){

您必须在.clear()之后调用location list函数。我将.clear()放在了错误的位置。我没有创建mapOf,而是使用类PlaceDetails来分配每个位置信息,而是使用mutableListOf()进行了调用。在for循环中,只需调用为其指定的可变列表名称。半径以米为单位,您可以将其更改为所需的半径。这大约是150英里的raidus

我使用circle.center作为圆心的板条,具有:

.center(mMap.cameraPosition.target)

一旦您将其放置在OnCameradle中,这将在每个摄像头空闲时更新中心板条。

我已经查看了迭代器()。我已经尝试创建一个私有val列表('location1','location2'))但是它声明Required:Marker Found:String。我知道'locaton1'和'2'是字符串。所以我尝试创建一个类似mMap.addMarker(MarkerOptions().position(LatLng(34.695779,-110.310185)).title的标记(“location1”但它又出现了另一个错误。我找不到任何关于如何在线创建listOf的信息。我只是看到人们在代码段中使用它的地方。我做了一些代码更改。我将更新主要问题。我现在可以循环使用我的mutableListOf,但它没有显示标记。我已经确定它确实由于日志而运行该块。我但它不会显示/隐藏标记。