Offline Caching for the MapBox iOS SDK

May 04 2012 by Justin Miller

We now offer unrestricted control for caching MapBox Streets on mobile devices with the MapBox iOS SDK, supporting both app developers who want fine-grained control of app storage size and performance, as well as for users who want the optimum use of bandwidth and the best performance for the maps in an app. In short, you can cache MapBox Streets custom styled maps (see how to put these into your app in five minutes in our latest screencast) as much as you’d like, for as long as you’d like, which is unprecedented compared to the policies of other map providers.

This will be huge whether you run a check-in service where users most often use maps around their home city, a role-playing game where you are defending your local turf, or a travel app where you want to select a region and take the map offline. This is all part of our larger platform push, supporting great developers by giving them total control of their maps.

Here’s the relevant section of the terms of service:

Proxying and/or intermediate caching of MapBox Maps is prohibited. Mobile clients may cache MapBox Maps on consumer devices for offline use and performance purposes. Further redistribution from a cache is prohibited.

How tiles are loaded

When a map view is shown and the user starts panning and zooming around to explore an area, the SDK requests the necessary tiles based on the layers that are shown. For example, maybe it’s just MapBox Streets that’s shown currently (check out how easy it is to get that in your app). The RMMapBoxSource tile source checks its local cache of tiles before ever trying to request data over the network, so if a tile has been downloaded recently, it is instantly recalled and used to reload into the map.

How the tile cache works

By default, the Route-Me rendering engine that our SDK is built upon stores the 1,000 most recently downloaded tile images. Once more room is needed, the oldest tiles are deleted from cache. The developer can also remove all tiles whenever desired with RMMapView’s aptly-named removeAllCachedImages method. But we’ve also added the capability to manage the tile cache based on length of time the tiles are in the cache.

For example, creating the RMTileCache with an expiryPeriod of 60 seconds/minute x 60 minutes/hour x 24 hours/day x 7 days = 604,800 will cause tiles to stay in the cache no longer than a week, without regard to the number of tiles, before the SDK will automatically delete the older tiles to keep things tidy.

Many potential uses

Allowing the developer this kind of control, as well as specifically enforcing it in our terms of service, really changes the traditional power balance in this type of scenario. The developer has complete control over giving their users offline capabilities, as well as refreshing map styles based on changes by the developer whenever they want, either with app updates or based on time constraints. The power really is in the developer’s hands.