From f6522d37c1418933207ee9a54d08222fcf264334 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 14 Oct 2022 16:59:44 +0200 Subject: [PATCH] fix(google-maps): allow to resize icons with same image (#1228) --- .../plugins/googlemaps/CapacitorGoogleMap.kt | 40 ++++++++++--------- google-maps/ios/Plugin/Map.swift | 30 ++++++-------- 2 files changed, 34 insertions(+), 36 deletions(-) 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 d7c264c11..8454c097b 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 @@ -46,7 +46,7 @@ class CapacitorGoogleMap( private var mapView: MapView private var googleMap: GoogleMap? = null private val markers = HashMap() - private val markerIcons = HashMap() + private val markerIcons = HashMap() private var clusterManager: ClusterManager? = null private val isReadyChannel = Channel() @@ -543,8 +543,8 @@ class CapacitorGoogleMap( if (!marker.iconUrl.isNullOrEmpty()) { if (this.markerIcons.contains(marker.iconUrl)) { - val cachedIcon = this.markerIcons[marker.iconUrl] - markerOptions.icon(cachedIcon) + val cachedBitmap = this.markerIcons[marker.iconUrl] + markerOptions.icon(getResizedIcon(cachedBitmap!!, marker)) } else { try { var stream: InputStream? = null @@ -554,22 +554,8 @@ class CapacitorGoogleMap( stream = this.delegate.context.assets.open("public/${marker.iconUrl}") } var bitmap = BitmapFactory.decodeStream(stream) - - if (marker.iconSize != null) { - bitmap = - Bitmap.createScaledBitmap( - bitmap, - (marker.iconSize!!.width * this.config.devicePixelRatio) - .toInt(), - (marker.iconSize!!.height * this.config.devicePixelRatio) - .toInt(), - false - ) - } - - val icon = BitmapDescriptorFactory.fromBitmap(bitmap) - this.markerIcons[marker.iconUrl!!] = icon - markerOptions.icon(icon) + this.markerIcons[marker.iconUrl!!] = bitmap + markerOptions.icon(getResizedIcon(bitmap, marker)) } catch (e: Exception) { var detailedMessage = "${e.javaClass} - ${e.localizedMessage}" if (marker.iconUrl!!.endsWith(".svg")) { @@ -591,6 +577,22 @@ class CapacitorGoogleMap( return markerOptions } + private fun getResizedIcon(_bitmap: Bitmap, marker: CapacitorGoogleMapMarker): BitmapDescriptor { + var bitmap = _bitmap + if (marker.iconSize != null) { + bitmap = + Bitmap.createScaledBitmap( + bitmap, + (marker.iconSize!!.width * this.config.devicePixelRatio) + .toInt(), + (marker.iconSize!!.height * this.config.devicePixelRatio) + .toInt(), + false + ) + } + return BitmapDescriptorFactory.fromBitmap(bitmap) + } + fun onStart() { mapView.onStart() } diff --git a/google-maps/ios/Plugin/Map.swift b/google-maps/ios/Plugin/Map.swift index 80bcfadf4..023bd83ed 100644 --- a/google-maps/ios/Plugin/Map.swift +++ b/google-maps/ios/Plugin/Map.swift @@ -381,30 +381,18 @@ public class Map { // cache and reuse marker icon uiimages if let iconUrl = marker.iconUrl { if let iconImage = self.markerIcons[iconUrl] { - newMarker.icon = iconImage + newMarker.icon = getResizedIcon(iconImage, marker) } else { if iconUrl.starts(with: "https:") { DispatchQueue.main.async { if let url = URL(string: iconUrl), let data = try? Data(contentsOf: url), let iconImage = UIImage(data: data) { - if let iconSize = marker.iconSize { - let resizedIconImage = iconImage.resizeImageTo(size: iconSize) - self.markerIcons[iconUrl] = resizedIconImage - newMarker.icon = resizedIconImage - } else { - self.markerIcons[iconUrl] = iconImage - newMarker.icon = iconImage - } + self.markerIcons[iconUrl] = iconImage + newMarker.icon = getResizedIcon(iconImage, marker) } } } else if let iconImage = UIImage(named: "public/\(iconUrl)") { - if let iconSize = marker.iconSize { - let resizedIconImage = iconImage.resizeImageTo(size: iconSize) - self.markerIcons[iconUrl] = resizedIconImage - newMarker.icon = resizedIconImage - } else { - self.markerIcons[iconUrl] = iconImage - newMarker.icon = iconImage - } + self.markerIcons[iconUrl] = iconImage + newMarker.icon = getResizedIcon(iconImage, marker) } else { var detailedMessage = "" @@ -425,6 +413,14 @@ public class Map { } } +private func getResizedIcon(_ iconImage: UIImage, _ marker: Marker) -> UIImage? { + if let iconSize = marker.iconSize { + return iconImage.resizeImageTo(size: iconSize) + } else { + return iconImage + } +} + extension WKWebView { override open func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { var hitView = super.hitTest(point, with: event)