9

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?

Recusiwe
  • 1,594
  • 4
  • 31
  • 54
  • I have the same problem. There's a few suggestions in other answers: clustering (https://stackoverflow.com/questions/29390359/custom-marker-performance-ios-crash-with-result-null-was-false-reached-th), reusing the Images (https://stackoverflow.com/questions/25555778/optimising-custom-marker-image-performance-with-google-maps-sdk-for-ios). Apparently for different images there's not much to be done other than restrict them, for eg: with the zoom level (https://stackoverflow.com/questions/43910085/googlemaps-sdk-for-ios-swift-3-when-hiding-a-marker-and-then-adding-the-map-v) – Efren Jul 13 '17 at 01:05
  • Yea, I ended up reducing the amount of images by restricting zoom level – Recusiwe Jul 21 '17 at 14:20

3 Answers3

5

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. If the images are dynamic, you could merge the images in code and just use one single overlay in Maps.

lax1089
  • 3,403
  • 3
  • 17
  • 37
4

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
    }
}
Eugenio
  • 1,013
  • 3
  • 13
  • 33
Bhas
  • 51
  • 2
3

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)
}
Alistra
  • 5,177
  • 2
  • 30
  • 42