Setting an icon to GMSMarker
in Google Maps SDK
requires the UIImage
, but currently my requirements are downloading it from a specific URL
Issue
The problem is that somehow only the last item sometimes is being shown. This the code on how I create markers (Updated in Swift)
func createMarkers() {
mapView.clear()
let mapCoordinates: [CLLocationCoordinate2D] = coordinates()
let iconURLs: [URL] = imageURLs()
var marker = GMSMarker()
for i in 0..<mapCoordinates.count {
let imageURL = iconURLs[i]
marker = GMSMarker()
marker.position = mapCoordinates[i]
marker.map = mapView
downloadImage(from: imageURL) { image in
marker.icon = image
}
}
}
// It is a helper function calling `SDWebImage` which caches the `UIImage` based on its `URL` string
func downloadImage(from url: URL, completion: @escaping (UIImage) -> Void)
From code provided above, I am having trouble while I am loading the data first time, because pins are showing on map but without image. If I call createMarkers()
again after some time, the icons are loaded correctly.
I don't know why this is happening, any suggestion or hint to make fix this issue?