My current code for adding overlays to my MapView is extremely slow when zooming. Tiles will take about 2 or 3 seconds to initially render. I was wondering if there was a way to call the API I'm using to cache all tiles for every zoom level when the app is opened, before a user would start zooming in on the map. Or if there are any other suggestions for efficiency I would be happy to hear them. Thanks!
Here is my code that changes the overlay shown every .5 seconds
@objc func changeOverlay() {
if(currentOverlay == 9) {
currentOverlay = 0
}
let allOverlays = mapView.overlays
mapView.removeOverlays(allOverlays)
overlayTimeLabel.text = overlayTimeArray[currentOverlay]
if(overlays.count == 9) {
mapView.addOverlay(europeOverlays[currentOverlay], level: .aboveRoads)
mapView.addOverlay(northAmericaOverlays[currentOverlay], level: .aboveRoads)
currentOverlay += 1
}
}
and here is my code for renderer
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKTileOverlayRenderer(overlay:overlay)
renderer.alpha = 0.8
return renderer
}
I have seen similar effects.
Apples default tile size is 256; servers usually like to serve and cache tile sizes of 512 because popular web libraries do it that way. My theory is that server don't cache and/or render smaller tile sizes on the fly, because they are usually not used. No matter if my theory is correct, 512 is faster in my tests.
This is how I do it:
There might be better places to set the tile size, but this works. You might have to tell the server that you want tiles of size 512, depending on what your server wants.