I am using @capacitor/google-maps
plugin for Google Maps SDK integration in my Capacitor app.
I encountered an erratic behavior with markers on iOS implementation only (on Android it works perfectly). The markers are loading (sometimes wrong icons are shown), and when I update their position (in an attempt to debug this issue), they seem to swap icons and artefacting all over the place.
This is so weird that I’m adding a video, maybe someone from Google could possibly explain what could go so wrong with the iOS SDK and/or plugin implementation: https://youtube.com/shorts/nkSTDlhLkcg?feature=share
I attach some relevant code (Swift), with some additions that are not in the original plugin:
// function to update marker position
func updateMarkerPosition(id: String, position: LatLng) throws {
if let marker = self.markers[id] {
DispatchQueue.main.sync {
marker.position = CLLocationCoordinate2D(latitude: position.lat, longitude: position.lng)
}
} else {
throw GoogleMapErrors.markerNotFound
}
}
private func decodeBase64ToUIImage(base64String: String) -> UIImage? {
guard let data = Data(base64Encoded: base64String, options: .ignoreUnknownCharacters) else {
return nil
}
return UIImage(data: data)
}
// function to attach a base64 image as a marker icon
private func buildMarker(marker: Marker) -> GMSMarker {
let newMarker = GMSMarker()
newMarker.position = CLLocationCoordinate2D(latitude: marker.coordinate.lat, longitude: marker.coordinate.lng)
newMarker.title = marker.title
newMarker.snippet = marker.snippet
newMarker.isFlat = marker.isFlat ?? false
newMarker.opacity = marker.opacity ?? 1
newMarker.isDraggable = marker.draggable ?? false
newMarker.zIndex = marker.zIndex
if let iconAnchor = marker.iconAnchor {
newMarker.groundAnchor = iconAnchor
}
if let iconUrl = marker.iconUrl {
if iconUrl.starts(with: "data:image") {
let base64String = iconUrl.components(separatedBy: ",").last ?? ""
if let base64Image = decodeBase64ToUIImage(base64String: base64String) {
print("Adding new icon for marker: (marker.uniqueID)")
newMarker.icon = getResizedIcon(base64Image, marker)
}
}
}
// function to add a marker on the map
func addMarker(marker: Marker) throws -> String {
var markerHash = "nil"
DispatchQueue.main.sync {
let newMarker = self.buildMarker(marker: marker)
newMarker.map = self.mapViewController.GMapView
self.markers[marker.uniqueID] = newMarker
markerHash = marker.uniqueID
}
return markerHash
}