Map not clickable around marker in Google Map SDK for Android

无人久伴 提交于 2019-12-06 04:10:14

You have to use Overlay instead of marker in the Map to get exactly what you desire. You could follow this link, similar is done in JavaScript here.

I found a way to manually handle clicks for markers.

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
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!