Android版Google Map SDK中标记周围的地图不可点击
我正在构建一些类似下图的应用程序,我想强制标记不可单击,但标记或标记选项没有可设置可单击(false)。 当前标记周围的区域(见附件)不可单击(单击传递给标记,而不是映射)Android版Google Map SDK中标记周围的地图不可点击,android,click,google-maps-markers,google-maps-android-api-2,Android,Click,Google Maps Markers,Google Maps Android Api 2,我正在构建一些类似下图的应用程序,我想强制标记不可单击,但标记或标记选项没有可设置可单击(false)。 当前标记周围的区域(见附件)不可单击(单击传递给标记,而不是映射) 您必须在地图中使用覆盖而不是标记,以获得您想要的内容。您可以点击这个链接,JavaScript中也有类似的操作。我找到了一种手动处理标记点击的方法 按照此stackoverflow答案中的说明添加可触摸包装: 在你的片段中添加一个手势检测器,听单个敲击声,然后根据lat lng找到最近的标记: private var ges
您必须在地图中使用覆盖而不是标记,以获得您想要的内容。您可以点击这个链接,JavaScript中也有类似的操作。我找到了一种手动处理标记点击的方法 按照此stackoverflow答案中的说明添加可触摸包装: 在你的片段中添加一个手势检测器,听单个敲击声,然后根据lat lng找到最近的标记:
private var gestureDetector: GestureDetector? = null
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
gestureDetector = GestureDetector(context, GoogleMapsGestureListener { e -> onMapSingleTap(e) })
//id of touchable wrapper - can use findViewById here instead if not using kotlin synthetics
googleMapsTouchableWrapper?.onTouch = {
gestureDetector?.onTouchEvent(it)
}
}
private fun onMapSingleTap(e: MotionEvent) {
val latLng = map?.projection?.fromScreenLocation(Point(e.x.toInt(), e.y.toInt())) ?: return
//this assumes you are maintaining a set of the latlngs for your markers
val closestNearbyLatLng = markerLatLngs?.findClosestNearbyLatLng(latLng)
//assuming you have a map of latlng to marker you can now find that marker based on latlng and do whatever you need to with it
}
private fun Set<LatLng>.findClosestNearbyLatLng(latLng: LatLng): LatLng? {
val map = map ?: return null
val screenDistance = map.projection.visibleRegion.latLngBounds.northeast.distanceBetweenInKm(map.projection.visibleRegion.latLngBounds.southwest)
val closestLatLng = this.minBy { latLng.distanceBetweenInKm(it) } ?: return null
if (latLng.distanceBetweenInKm(closestLatLng) < screenDistance/40) {
return closestLatLng
}
return null
}
fun LatLong.distanceBetweenInKm(latLng: LatLng): Double {
if (this == latLng) {
return 0.0
}
val earthRadius = 6371.0 //km value;
//converting to radians
val latPoint1Radians = Math.toRadians(latitude)
val lngPoint1Radians = Math.toRadians(longitude)
val latPoint2Radians = Math.toRadians(latLng.latitude)
val lngPoint2Radians = Math.toRadians(latLng.longitude)
var distance = sin((latPoint2Radians - latPoint1Radians) / 2.0).pow(2.0) + (cos(latPoint1Radians) * cos(latPoint2Radians)
* sin((lngPoint2Radians - lngPoint1Radians) / 2.0).pow(2.0))
distance = 2.0 * earthRadius * asin(sqrt(distance))
return abs(distance) //km value
}
class GoogleMapsGestureListener(private val onSingleTap: (MotionEvent) -> Unit) : GestureDetector.SimpleOnGestureListener() {
override fun onSingleTapConfirmed(e: MotionEvent?): Boolean {
super.onSingleTapConfirmed(e)
e?.let { onSingleTap(it) }
return true
}
}
专用变量gestureDetector:gestureDetector?=无效的
...
覆盖已创建的视图(视图:视图,保存状态:捆绑?){
super.onViewCreated(视图,savedInstanceState)
gestureDetector=gestureDetector(上下文,GoogleMapsGestureListener{e->onMapSingleTap(e)})
//可触摸包装的id-如果不使用kotlin synthetics,可以在此处使用findViewById
GoogleMapsToTouchableWrapper?.onTouch={
手势检测器?.onTouchEvent(it)
}
}
Mopsingletap上的私人娱乐(e:MotionEvent){
val latLng=map?projection?from屏幕位置(点(e.x.toInt(),e.y.toInt())?:返回
//这假设您正在为标记维护一组latlngs
val closestNearbyLatLng=markerLatLngs?.findClosestNearbyLatLng(latLng)
//假设你有一张latlng到marker的地图,你现在可以根据latlng找到那个marker,并用它做任何你需要的事情
}
私人娱乐设施。查找附近的停车场(latLng:latLng):latLng?{
val map=map?:返回空值
val screenDistance=map.projection.visibleRegion.latLngBounds.northeast.DistanceBetweenkm(map.projection.visibleRegion.latLngBounds.southwest)
val closestLatLng=this.minBy{latLng.distancebetweenninkm(it)}?:返回null
if(间隔距离在公里之间(闭合间隙)<屏幕距离/40){
回程闭锁
}
返回空
}
有趣的拉特朗。公里之间的距离(拉特朗:拉特朗):双倍{
如果(此==latLng){
返回0.0
}
val地球半径=6371.0//km值;
//转换成弧度
val latPoint1Radians=数学上的toRadians(纬度)
val lngPoint1Radians=数学半径(经度)
val latPoint2Radians=数学toRadians(纬度)
val lngPoint2Radians=数学toRadians(纬度经度)
var距离=sin((latPoint2Radians-latPoint1Radians)/2.0)。功率(2.0)+(cos(latPoint1Radians)*cos(latPoint2Radians)
*sin((lngPoint2Radians-lngPoint1Radians)/2.0).pow(2.0))
距离=2.0*接地半径*asin(sqrt(距离))
返回abs(距离)//km值
}
类GoogleMapsGestureListener(私有值onSingleTap:(MotionEvent)->Unit):GestureDetector.SimpleOnGestureListener(){
override fun OnSingleTapConfiged(e:MotionEvent?):布尔值{
super.OnSingletAPconfirm(e)
让{onSingleTap(it)}
返回真值
}
}
这项功能似乎是在Google issue tracker中请求的: