Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(google-maps): Optimize addMarkers method for improved performance #1997

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -182,27 +182,26 @@ class CapacitorGoogleMap(
val markerIds: MutableList<String> = mutableListOf()

CoroutineScope(Dispatchers.Main).launch {
newMarkers.forEach {
val markerOptions: Deferred<MarkerOptions> =
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))
Expand Down