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

fix(google-maps): allow to resize icons with same image #1228

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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