7

Does Mapkit on IOS support dequeuing of overlays? (like it does annotations). If so what is the code for achieving this?

Background: With annotations I believe you can add many, leaving MapKit to instantiate views when required for them via the dequeuing approach. What about the same thing for overlays however if I have many across the country? Do I need to write the code for checking which overlays I have are visible or not and then instantiate them / remove them myself in realtime?

Greg
  • 34,042
  • 79
  • 253
  • 454
  • PS. Also whether people are actually (if this is an issue) putting overlays on a map as an "annotation", to get around this? Probably wouldn't work for large overlays however as part of the overlay might be offscreen.... – Greg May 20 '16 at 04:51
  • PSS. Actually have about 8000 or so overlays across the country to put in place, so ultimately do I load them all in upfront, or do I need to manage them myself manually such that only a much smaller number are loaded at any one point in time? i.e. the ones that would be visible – Greg May 25 '16 at 10:00

1 Answers1

3

Map Kit does not support reuse of overlays in the same way it supports doing this for annotation views. One of the reasons for this must certainly be that the two objects are not analogous. Overlays are model objects that represent an area on the map, whereas annotation views are view objects used from time to time to display the location of an annotation on the map. The technique of reusing view objects (as opposed to creating individual ones for every use) is an optimization that is used in a couple of other places in UIKit, notably for Table View Cells and various bits of Collection Views.

The view reuse pattern is to establish some data index (table index paths, map coordinates) and then have a delegate provide an appropriate view object to use when a particular index/location comes into view. When the data object passes out of sight, the view object is recycled in a queue.

An annotation is analogous to an overlay, and MapKit does not provide reuse for them either and for good reason: they are the data that is being displayed!

The analogous object to the annotation view is an overlay renderer, which (of course!) provides rendering for an overlay. I assume that the reason these are not reused is because they are not view system objects and presumably much more lightweight, so there is little benefit from reuse. We find evidence for this in the fact that until iOS 7.0 the MapView delegate did provide a view object for overlays and this was replaced by the renderer concept.

I hope that helps.

What problem is this causing for you?

BrentM
  • 249
  • 1
  • 4
  • I have several hundred overlays to put across the country. Is the idea that you can load a large number up and then just leave it to MapView to optimize what needs to be displayed? May need to add these off the main thread so the start up time when the app starts isn't noticeable to the user then I guess? Also there is the question of how to get a list of visible overlays only for which I have a question here: http://stackoverflow.com/questions/37433336/how-to-get-visible-overlays-in-mapkit-i-e-mkoverlay-mkoverlayrender-from-a-ma – Greg May 25 '16 at 09:45
  • PS Actually have about 8000 or so overlays across the country to put in place, so ultimately do I load them all in upfront, or do I need to manage them myself manually such that only a much smaller number are loaded at any one point in time? i.e. the ones that would be visible – Greg May 25 '16 at 10:00
  • 8000 is a big number. I did a test with 7200 overlays spread over the US and saw noticeable sluggishness on an iPhone 5 /iOS 9.3. Doing some sort of tiling will be necessary for decent performance. However, I took a simple stab at that and it wasn't particularly promising. It looks like the operation of adding or removing large numbers of overlays itself is not very fast. Perhaps you could reorganize your data so that each MKOverlay represents a tile of 'overlays' within a region of the map. Your overlay renderer would need to be smart about drawing the appropriate data items. – BrentM May 25 '16 at 21:35
  • To answer your original question, I do think you'll need to roll your own solution. I notice there's a class called `MKTileOverlay` which although it has a different intent (loading tiles from a server) might be able to be leveraged to do the work of deciding what tiles are necessary. – BrentM May 25 '16 at 21:35