fredag 28 december 2012

Using maps offline in Android and iOS part 1


In some apps offline maps are needed in order to use the app without internet connectivity. The Google Maps API for Android and the MapKit API for iOS currently doesn't support offline maps but there are other options.

Instead of using maps from Google or Apple it's possible to use map data from the Open Street Map (OSM) project. The OSM data is collected by people around the world and is free for both non-commercial and commercial use.

Rendering the tiles

Before the OSM data can be used as a map it needs to be rendered into tiles. One tile is usually a 250x250 pixels PNG image and a map consists of several hundreds or thousands tiles. Rendering tiles requires a lot of resources, which is why the tiles available at the Open Street Map website are not free to download.

There are some companies such as MapBox that provides an API to download rendered OSM tiles for a few dollars every month. But if the tiles are going to be bundled with the app and used offline you can render the tiles your self using Open Source Software.

If you are running Windows or OS X you can install Ubuntu in a VirtualBox and follow the instructions at the switch2osm website to setup your own tile server. The tile server will render tiles using the default OSM Mapnik stylesheet using raw OSM data that you provide. When running the tile server in a virtual machine it's a good idea to use a OSM extract from Geofabrik to reduce the rendering time.

Using the tiles

When the tile server is up and running you should be able to access it from a web browser. If the tile server is running in a virtual machine make sure you use a bridged network configuration.

To use the map offline in an app all the tiles could be downloaded from the tile server and stored in the app as images. However, accessing thousands of images in the file system when browsing the map is not great for performance. A better solution is to use a SQLite database to store the tiles in a single file according to the MBTiles specification.

MBTiles can be created using the Mobile Atlas Creator which supports different map sources. By adding your own tile server as a custom map source it can be used to browse your map. Select a region in the map, the number of zoom levels and then export the atlas using the MBTiles format. If the exported file is to large modify the region or number of zoom levels and try again.


Using the Google Maps API or MapKit API is simple and recommended for most apps. If your app requires offline support more work is required.

In this post we have focused on the steps required to render tiles and save them as a map in the MBTiles format. The map can then be used by third-party libraries for both Android and iOS which will be covered in upcoming posts.

Inga kommentarer:

Skicka en kommentar