maps: click listeners on polylines and polygons

This commit is contained in:
Fynn Godau 2026-03-27 15:01:03 +01:00 committed by Marvin W
parent cd1f5b8997
commit 5abd293186
4 changed files with 66 additions and 2 deletions

View file

@ -32,6 +32,8 @@ abstract class AbstractGoogleMap(context: Context) : IGoogleMapDelegate.Stub() {
internal var mapLongClickListener: IOnMapLongClickListener? = null
internal var markerClickListener: IOnMarkerClickListener? = null
internal var circleClickListener: IOnCircleClickListener? = null
internal var polygonClickListener : IOnPolygonClickListener? = null
internal var polylineClickListener : IOnPolylineClickListener? = null
internal var myLocationChangeListener: IOnMyLocationChangeListener? = null
@ -87,11 +89,11 @@ abstract class AbstractGoogleMap(context: Context) : IGoogleMapDelegate.Stub() {
}
override fun setOnPolygonClickListener(listener: IOnPolygonClickListener?) {
Log.d(TAG, "Not yet implemented: setOnPolygonClickListener")
polygonClickListener = listener
}
override fun setOnPolylineClickListener(listener: IOnPolylineClickListener?) {
Log.d(TAG, "Not yet implemented: setOnPolylineClickListener")
polylineClickListener = listener
}
override fun setOnGroundOverlayClickListener(listener: IOnGroundOverlayClickListener?) {

View file

@ -101,10 +101,12 @@ class GoogleMapImpl(context: Context, var options: GoogleMapOptions) : AbstractG
var lineManager: LineManager? = null
val pendingLines = mutableSetOf<Markup<Line, LineOptions>>()
val lines = mutableMapOf<Long, PolylineImpl>()
var lineId = 0L
var fillManager: FillManager? = null
val pendingFills = mutableSetOf<Markup<Fill, FillOptions>>()
val polygons = mutableMapOf<Long, PolygonImpl>()
val circles = mutableMapOf<Long, CircleImpl>()
var fillId = 0L
@ -760,6 +762,9 @@ class GoogleMapImpl(context: Context, var options: GoogleMapOptions) : AbstractG
})
fillManager.addClickListener { fill ->
try {
/* IDs are assigned consecutively across all types of fill, so no ID
* corresponds to both circle and polygon.
*/
circles[fill.id]?.let { circle ->
if (circle.isClickable) {
circleClickListener?.let {
@ -768,6 +773,29 @@ class GoogleMapImpl(context: Context, var options: GoogleMapOptions) : AbstractG
}
}
}
polygons[fill.id]?.let { polygon ->
if (polygon.isClickable) {
polygonClickListener?.let {
it.onPolygonClick(polygon)
return@addClickListener true
}
}
}
} catch (e: Exception) {
Log.w(TAG, e)
}
false
}
lineManager.addClickListener { line ->
try {
lines[line.id]?.let { polyline ->
if (polyline.isClickable) {
polylineClickListener?.let {
it.onPolylineClick(polyline)
return@addClickListener true
}
}
}
} catch (e: Exception) {
Log.w(TAG, e)
}
@ -860,8 +888,10 @@ class GoogleMapImpl(context: Context, var options: GoogleMapOptions) : AbstractG
userOnInitializedCallbackList.clear()
lineManager?.onDestroy()
lineManager = null
lines.clear()
fillManager?.onDestroy()
fillManager = null
polygons.clear()
circles.clear()
symbolManager?.onDestroy()
symbolManager = null

View file

@ -15,6 +15,7 @@ import com.google.android.gms.maps.model.PatternItem
import com.google.android.gms.maps.model.PolygonOptions
import com.google.android.gms.maps.model.PolylineOptions
import com.google.android.gms.maps.model.internal.IPolygonDelegate
import com.mapbox.mapboxsdk.plugins.annotation.AnnotationManager
import com.mapbox.mapboxsdk.plugins.annotation.Fill
import com.mapbox.mapboxsdk.plugins.annotation.FillOptions
import com.mapbox.mapboxsdk.utils.ColorUtils
@ -226,6 +227,20 @@ class PolygonImpl(private val map: GoogleMapImpl, id: String, options: PolygonOp
strokes.add(PolylineImpl(map, id, options))
}
override fun update(manager: AnnotationManager<*, Fill, FillOptions, *, *, *>) {
synchronized(this) {
val id = annotation?.id
if (removed && id != null) {
map.polygons.remove(id)
}
super.update(manager)
val annotation = annotation
if (annotation != null && id == null) {
map.polygons[annotation.id] = this
}
}
}
override fun onTransact(code: Int, data: Parcel, reply: Parcel?, flags: Int): Boolean = warnOnTransactionIssues(code, reply, flags) { super.onTransact(code, data, reply, flags) }
companion object {

View file

@ -12,6 +12,9 @@ import com.google.android.gms.maps.model.Cap
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.PatternItem
import com.google.android.gms.maps.model.internal.IPolylineDelegate
import com.mapbox.mapboxsdk.plugins.annotation.AnnotationManager
import com.mapbox.mapboxsdk.plugins.annotation.Fill
import com.mapbox.mapboxsdk.plugins.annotation.FillOptions
import com.mapbox.mapboxsdk.plugins.annotation.Line
import com.mapbox.mapboxsdk.plugins.annotation.LineOptions
import com.mapbox.mapboxsdk.utils.ColorUtils
@ -169,6 +172,20 @@ class PolylineImpl(private val map: GoogleMapImpl, id: String, options: GmsLineO
map.lineManager?.let { update(it) }
}
override fun update(manager: AnnotationManager<*, Line, LineOptions, *, *, *>) {
synchronized(this) {
val id = annotation?.id
if (removed && id != null) {
map.lines.remove(id)
}
super.update(manager)
val annotation = annotation
if (annotation != null && id == null) {
map.lines[annotation.id] = this
}
}
}
companion object {
private val TAG = "GmsMapPolyline"
}