如何解决在圆半径内显示地图标记隐藏半径迭代器问题之外的标记
我需要在我的圆半径内显示标记。我正在使用 Kotlin。我有每个 onCameraIdleListener 更新圆半径。我遇到的问题是我不明白如何正确使用 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)
)
}
}
}
这是我用于 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)
这是我为 Circle 设置的类:
class Circle(
val center: LatLng,val radius: Double,val strokeColor: Int,val fillColor: Int,val draggable: Boolean = false)
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)
)
}
}
}
以下是代码顶部的私有变量,用于更新相机的纬度/经度:
private var cameraLat: Double = 0.0
private var cameraLng: Double = 0.0
这是圆的地图:
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() {
for (marker : LatLng in circle.getValue("CIRCLE") ){ 如果(SphericalUtil.computedistanceBetween(circle.getValue(“CIRCLE”),test.getValue(“TESTLOCATION”))
这是我得到的错误“For-loop range must have an 'iterator()' method”这就是我被困的地方。我试图研究迭代器,但我不理解它。我的代码看起来正确吗?我的代码做错了吗?我从另一个类似问题的 comment 中得到了大致的想法。我相信这段代码是在java中的。我已尽最大努力使其保持相似。但我不确定如何迭代这个 for 循环?谢谢大家的帮助!
解决方法
当你调用 for 语句时,它应该是一个可迭代的集合,但你调用的是一个唯一的值,即 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
// 只需确保将可见设置为 false 以隐藏它们直到它们的圆半径
}
有趣的 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.")
}
}
}
然后在 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()
}
你必须在 .clear() 之后调用位置列表函数,我在错误的位置使用了 .clear()。而不是创建 mapOf 然后使用类 PlaceDetails 来分配每个位置信息,我只是使用 mutableListOf() 来完成的。在 for 循环中只需调用您为其分配的 mutableListOf 名称。半径以米为单位,您可以将其更改为您想要的半径。这大约是 150 英里的突袭。
我使用 circle.center 作为圆心的纬度,有:
.center(mMap.cameraPosition.target)
一旦你把它放在 onCameraIdle 中,这将在每个相机空闲时更新中心 LatLng。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。