Skip to content

Commit

Permalink
fix(google-maps): allow to resize icons with same image (#1228)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Oct 14, 2022
1 parent dd80da2 commit f6522d3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CapacitorGoogleMap(
private var mapView: MapView
private var googleMap: GoogleMap? = null
private val markers = HashMap<String, CapacitorGoogleMapMarker>()
private val markerIcons = HashMap<String, BitmapDescriptor>()
private val markerIcons = HashMap<String, Bitmap>()
private var clusterManager: ClusterManager<CapacitorGoogleMapMarker>? = null

private val isReadyChannel = Channel<Boolean>()
Expand Down Expand Up @@ -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
Expand All @@ -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")) {
Expand All @@ -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()
}
Expand Down
30 changes: 13 additions & 17 deletions google-maps/ios/Plugin/Map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""

Expand All @@ -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)
Expand Down

0 comments on commit f6522d3

Please sign in to comment.