Skip to content

Commit

Permalink
fix(marker): Fix markers not triggering onPress android
Browse files Browse the repository at this point in the history
  • Loading branch information
balloman authored Dec 15, 2023
1 parent 3764a55 commit c5368c5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-schools-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@balloman/expo-google-maps": patch
---

Fix onPress listener not triggering on android
22 changes: 18 additions & 4 deletions android/src/main/java/expo/modules/googlemaps/views/ExpoMapView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.GoogleMap.OnCameraIdleListener
import com.google.android.gms.maps.GoogleMap.OnCameraMoveListener
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.LatLngBounds
import com.google.android.gms.maps.model.MapStyleOptions
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.Polygon
import com.google.android.gms.maps.model.PolygonOptions
import expo.modules.googlemaps.AnimateOptions
Expand All @@ -25,10 +27,11 @@ import expo.modules.kotlin.views.ExpoView

@SuppressLint("ViewConstructor")
class ExpoMapView(context: Context, appContext: AppContext) : ExpoView(context, appContext),
OnMapReadyCallback, OnCameraIdleListener, OnCameraMoveListener {
OnMapReadyCallback, OnCameraIdleListener, OnCameraMoveListener, OnMarkerClickListener {
private var polygonMap: MutableMap<String, Polygon> = mutableMapOf()
private var tempPolygons: List<PolygonRecord> = listOf()
private var tempMarkers: MutableList<ExpoMarkerView> = mutableListOf()
private var markers: MutableMap<Marker, ExpoMarkerView> = hashMapOf()
var googleMap: GoogleMap? = null

// We need to store the polygons in a temp variable until the map is ready
Expand Down Expand Up @@ -59,13 +62,13 @@ class ExpoMapView(context: Context, appContext: AppContext) : ExpoView(context,
return
}
child.gmsMarker = marker
child.gmsMap = googleMap
markers[marker] = child
return
}
super.onViewAdded(child)
}

@SuppressLint("MissingPermission")
@SuppressLint("MissingPermission", "PotentialBehaviorOverride")
override fun onMapReady(map: GoogleMap) {
Log.e("ExpoGoogleMapsModule", "onMapReady")
googleMap = map
Expand All @@ -88,10 +91,11 @@ class ExpoMapView(context: Context, appContext: AppContext) : ExpoView(context,
return
}
markerView.gmsMarker = marker
markerView.gmsMap = googleMap
markers[marker] = markerView
}
googleMap!!.setOnCameraIdleListener(this)
googleMap!!.setOnCameraMoveListener(this)
googleMap!!.setOnMarkerClickListener(this)
}

override fun onCameraIdle() {
Expand All @@ -110,6 +114,16 @@ class ExpoMapView(context: Context, appContext: AppContext) : ExpoView(context,
)
}

override fun onMarkerClick(p0: Marker): Boolean {
val markerView = markers[p0]
if (markerView == null) {
Log.e("ExpoGoogleMapsModule", "Failed to find marker")
return false
}
markerView.onMarkerPress(mapOf())
return false
}

/**
* Updates the polygons on the map, making sure to only add, remove, or update the polygons that have changed
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import android.graphics.Bitmap
import android.graphics.Canvas
import android.view.View
import androidx.core.view.isVisible
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.MarkerOptions
import expo.modules.googlemaps.MarkerRecord
import expo.modules.kotlin.AppContext
import expo.modules.kotlin.viewevent.EventDispatcher
import expo.modules.kotlin.views.ExpoView

@SuppressLint("ViewConstructor")
class ExpoMarkerView(context: Context, appContext: AppContext) : ExpoView(context, appContext) {
private var markerRecord: MarkerRecord? = null
var gmsMarker: Marker? = null
var gmsMap: GoogleMap? = null
private var bitmap: Bitmap? = null
private var child: View? = null
val onMarkerPress by EventDispatcher()

fun updateMarker(marker: MarkerRecord) {
markerRecord = marker
Expand Down

0 comments on commit c5368c5

Please sign in to comment.