I'm building an app which uses Google Maps, and a lot of overlays, is seems like when I try to load a lot overlays it stall and provide me with "((null)) was false: Reached the max number of texture atlases, can not allocate more."
I'm just adding images as overlays this way:
...
if (image != nil) {
let image: CGImage = (image?.cgImage)!
let icon = UIImage(cgImage: image)
let overlay = GMSGroundOverlay(bounds: overlayBounds, icon: icon)
overlay.bearing = 0
overlay.map = map
overlay.zIndex = 10
self.overlays.append(overlay);
Any suggestions on how to fix this issue?
The problem appears to be that you are allocating a separate UIImage instance to each marker/overlay. This means that when you are plotting markers on the GMSMapView instance, each marker has a separate UIImage.
If you are creating several markers with the same image, use the same instance of UIImage for each of the markers. This helps improve the performance of your application when displaying many markers.
Simply put, you need to limit the overlays you append. Are your images dynamic or static? If it is the same image a simple solution is to define the image once and just add a reference to the same image when you append the overlay.
Instead of setting marker's iconView, set marker's icon. That too initializes the image outside of the for loop:
func displayMarkers() {
let iconImage = UIImage(named: "locationgreen")
for partner in partners {
let lat : Double = Double(partner.location?.coordinates![1] ?? 0)
let lng : Double = Double(partner.location?.coordinates![0] ?? 0)
let position = CLLocationCoordinate2D(latitude: lat, longitude: lng)
let marker = GMSMarker(position: position)
marker.title = partner.name
marker.icon = iconImage
}
}
Try
if let image = image {
let overlay = GMSGroundOverlay(bounds: overlayBounds, icon: icon)
overlay.bearing = 0
overlay.map = map
overlay.zIndex = 10
self.overlays.append(overlay)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With