kotlin 标记占用太大的点击区域

xmq68pz9  于 2023-08-06  发布在  Kotlin
关注(0)|答案(1)|浏览(133)

我想减少。触摸标记的焦点区域。虽然点击面积太大,我想缩短谷歌Map上标记的点击面积。
我是新的Map,我想知道我们如何才能减少填充标记在重点在android。

7y4bm7vi

7y4bm7vi1#

  • 我先试过了。在xml <View android:id="@+id/transparentOverlay" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" />中添加一个覆盖
  1. then inside onMapReady, I implemented this code -
  2. binding.transparentOverlay.setOnClickListener { view ->
  3. val mapClickPoint = Point(view.x.toInt(), view.y.toInt())
  4. val mapLatLng = map.projection.fromScreenLocation(mapClickPoint)
  5. if (this::carMarker.isInitialized) {
  6. if (isPointInMarkerClickableArea(carMarker, mapLatLng)) {
  7. // The click was within the custom clickable area of the marker
  8. // Handle the marker click here
  9. markerClickAction(carMarker)
  10. }
  11. }
  12. } private fun isPointInMarkerClickableArea(marker: Marker, clickLatLng: LatLng): Boolean {
  13. // Calculate the distance between the marker's position and the clickLatLng
  14. // If the distance is within a certain threshold, consider it a click on the marker
  15. val distanceThreshold = 50 // in pixels, adjust as needed
  16. val markerPosition = marker.position
  17. val markerScreenPosition = map.projection.toScreenLocation(markerPosition)
  18. val clickScreenPosition = map.projection.toScreenLocation(clickLatLng)
  19. val distance = Math.sqrt(Math.pow((markerScreenPosition.x - clickScreenPosition.x).toDouble(), 2.0) +
  20. Math.pow((markerScreenPosition.y - clickScreenPosition.y).toDouble(),
  21. 2.0))
  22. return distance <= distanceThreshold } private fun markerClickAction(carMarker: Marker) {
  23. // Implement your action when the marker is clicked
  24. // For example, show an info window, navigate to a detailed view, etc.
  25. Toast.makeText(this, "Click on marker: ${carMarker.title}", Toast.LENGTH_SHORT).show() }

字符串

展开查看全部

相关问题