I have recently been incorporating the Google Maps for iOS SDK within an iOS app. This app is designed to retrieve position of aircraft (including aircraft model, lat/lng, s
Instead of setting marker's iconView, set marker's icon. That too initialize the image outside of for loop, as below
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
}
}
Answering my own question after coming across the issue again.
The problem appears to be that I am allocating a separate UIImage instance to each marker. This means that when I am plotting markers on the GMSMapView instance, each marker has a separate UIImage. This is described briefly here: Customize the marker image - Google Maps SDK for iOS.
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.
I was iterating through a list of objects to create each marker:
for (int i = 0; i < [array count]; i++) {
UIImage *image = [UIImage imageWithContentsOfFile:@"image.png"];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(10, 10);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = @"Hello World";
marker.icon = image;
marker.map = mapView_;
}
So here, I was copying the image to each marker. That took up more resources than necessary. The solution for me:
UIImage *image = [UIImage imageWithContentsOfFile:@"image.png"];
for (int i = 0; i < [array count]; i++) {
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(10, 10);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = @"Hello World";
marker.icon = image;
marker.map = mapView_;
}
Defining the UIImage instance outside of the for-loop meant that the image was referenced from each marker, not re-rendered for each marker. Memory usage was much lower after this.
My solve; ((null)) was false: Reached the max number of texture atlases, can not allocate more.
You keep location information out of the thread while creating the markers.
OperationQueue.main.addOperation {
let coordinates = CLLocationCoordinate2D(latitude:LatData!, longitude: longData!)
let marker = GMSMarker(position: coordinates)
marker.icon = GMSMarker.markerImage(with: .blue)
for i in 0 ... self.DemandLong.count {
marker.infoWindowAnchor = CGPoint(x: 0, y: 5)
marker.map = self.MyExploreView
marker.accessibilityLabel = "\(i)" //Marker Label
print("Location Marker i:\(i)")
}
}