无法在Android版Google Map SDK中点击地图周围的标记 [英] Map not clickable around marker in Google Map SDK for Android

查看:122
本文介绍了无法在Android版Google Map SDK中点击地图周围的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建下面的图像之类的应用,我想强制标记不可单击,但是Marker或MarkerOptions没有setClickable(false)。
当前无法点击标记周围的区域(请参阅附件)(点击传递给标记,而不是地图)

I am building some app like image below, I want to force markers not to be clickable, but there is no setClickable(false) for Marker or MarkerOptions. Currently area around marker (see attachment) is not clickable ( click is passed to marker, not map)

推荐答案

我找到了一种手动处理标记点击的方法。

I found a way to manually handle clicks for markers.

按照以下stackoverflow答案中所述添加可触摸的包装器: https://stackoverflow.com/a/58039285/1499750

Add a touchable wrapper as described in this stackoverflow answer: https://stackoverflow.com/a/58039285/1499750

在片段中添加一个手势检测器,听一下轻按,然后根据经纬度找到最接近的标记:

Add a gesture detector to your fragment and listen to single taps, then find the closest marker based on 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
    }
}

这篇关于无法在Android版Google Map SDK中点击地图周围的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆