Skip to content
Nutiteq edited this page Aug 26, 2014 · 11 revisions

Mapsforge (www.mapsforge.org) is open source vector map rendering library. Vector files take significantly less storage space than raster map tiles in e.g. MBTiles format. However, rendering quality and speed on devices is not so high as with server-side rendered images.

Map generation

Mapsforge generates map based on same 256-pixel map tiles in EPSG:3857 as common online map sources, therefore we can use this instead of loading map tiles online. AdvancedLayers has data source MapsforgeRasterDataSource. For using it as base map layer you need to define rendering theme/style (as xml file) and map data source (.map file). So the steps would be:

  1. Copy both data .map file to the /mnt/sdcard folder. You can just take your home country .map file from AndroidMaps. For real application you probably want to create a proper manager/downloader for these, or bundle it in application assets folder.

  2. Add AdvancedLayers dependency to your project.

  3. Add following to your code to add map layer

        // use built-in render theme
        XmlRenderTheme renderTheme = InternalRenderTheme.OSMARENDER;

        MapDatabase mapDatabase = new MapDatabase();
        mapDatabase.closeFile();
        String mapFilePath = "/sdcard/mymapfile.map"; // set your path here
        File mapFile = new File("/" + mapFilePath);
        FileOpenResult fileOpenResult = mapDatabase.openFile(mapFile);
        if (fileOpenResult.isSuccess()) {
            Log.debug("MapsforgeRasterDataSource: MapDatabase opened ok: " + mapFilePath);
        }

        MapsforgeRasterDataSource dataSource = new MapsforgeRasterDataSource(new EPSG3857(), 0, 20, mapFile, mapDatabase, renderTheme, this.getApplication());
        RasterLayer mapLayer = new RasterLayer(dataSource, 1044);
        mapView.getLayers().setBaseLayer(mapLayer);
  1. Run your app, and make sure you are in location which is covered by the .map file. You should see map tiles rendered on the fly. It is not probably faster than online tiles, maybe even slower, but whole country map fits to your pocket.
Clone this wiki locally