diff --git a/google-maps/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMap.kt b/google-maps/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMap.kt index a22aa2208..b6b616b11 100644 --- a/google-maps/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMap.kt +++ b/google-maps/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMap.kt @@ -182,27 +182,26 @@ class CapacitorGoogleMap( val markerIds: MutableList = mutableListOf() CoroutineScope(Dispatchers.Main).launch { - newMarkers.forEach { - val markerOptions: Deferred = - CoroutineScope(Dispatchers.IO).async { - this@CapacitorGoogleMap.buildMarker(it) - } - val googleMapMarker = googleMap?.addMarker(markerOptions.await()) - it.googleMapMarker = googleMapMarker + val markerOptionsList = withContext(Dispatchers.IO) { + newMarkers.map { buildMarker(it) } + } - if (googleMapMarker != null) { + markerOptionsList.forEachIndexed { index, markerOptions -> + val googleMapMarker = googleMap?.addMarker(markerOptions) + val capacitorMarker = newMarkers[index] + googleMapMarker?.let { marker -> if (clusterManager != null) { - googleMapMarker.remove() + marker.remove() + } else { + markers[marker.id] = capacitorMarker + markerIds.add(marker.id) } - - markers[googleMapMarker.id] = it - markerIds.add(googleMapMarker.id) } } - if (clusterManager != null) { - clusterManager?.addItems(newMarkers) - clusterManager?.cluster() + clusterManager?.let { manager -> + manager.addItems(newMarkers) + manager.cluster() } callback(Result.success(markerIds))