This library includes a MapProvider that works with the Codename One MapComponent class to provide support for offline maps.
LGPL
- Should work on all CN1 platforms (J2ME, iOS, Android, BlackBerry, and Windows Phone).
- Works with the Codename One MapComponent
- Supports the
CN1Tiles
format for importing map data. Note: This format was created specifically for use by this module, however, you can use the CN1TileConverter tool to convert the more commonMBTiles
format into aCN1Tiles
file that can be used with this module.
- Install the CN1JTar library into your project.
- Copy the CN1OfflineMaps.cn1lib file into the lib directory of your codename one project.
- Right click on your project in the project explorer (if using Netbeans) and click "Refresh Libs".
MBTilesProvider provider;
if ( MBTilesProvider.isLoaded("output.cn1tiles") ){
// The map named "output.cn1tiles" is already loaded…
// We can just create a provider for this map.
provider = new MBTilesProvider("output.cn1tiles");
} else {
// The map specified hasn't been created yet…
// We can create this in a number of ways including:
// From resources (i.e. include the output.cn1tiles file as part of
// the application bundle):
if ( CREATE_FROM_RESOURCE ){
provider = MBTilesProvider.createFromResource("/output.cn1tiles");
} else if ( CREATE_FROM_FILE ){
// Or from the file system
provider = MBTilesProvider.createFromFile("/path/to/output.cn1tiles");
} else if ( CREATE FROM INPUTSTREAM ){
InputStream is = getInputStream(); // Could be from a server
provider = MBTilesProvider.create("output.cn1tiles", is);
}
}
// With our loaded MBTilesProvider let's create a MapComponent and add it to a form:
MapComponent map = new MapComponent(provider, provider.center(), provider.minZoomLevel(), false);
Form hi = new Form("Offline Map Test");
hi.setScrollable(false);
hi.setLayout(new BorderLayout());
hi.addComponent(BorderLayout.CENTER, map);
hi.show();
This libray is only useful if you are able to create the cn1tiles
file to begin with. In order to do this you will need the following tools:
- TileMill - A Desktop application that allows you to create maps using public data sources like Open Street Maps.
- CN1TileConverter - A Desktop utility that converts
mbtiles
files tocn1tiles
format. This was designed specifically to be used with this library.
Step 1: Create your map in TileMill.
Step 2: Export your map as an mbtiles
file.
Step 3: Convert your .mbtiles file to a .cn1tiles file using CN1TileConverter
Step 4: Load the .cn1tiles file inside your Codename One application usin the MBTilesProvider class. You can either embed the file as part of your application source, or place it on a server and have your application download it on first load (or some other logical time).