Offline Maps for Codename One
Java
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
dist
lib
nbproject
screenshots
src/ca/weblite/codename1/mapbox
README.md
Stubber.jar
build.xml
codenameone_library.properties
manifest.mf
manifest.properties Initial commit Jan 7, 2014

README.md

Codename Offline Maps Provider

This library includes a MapProvider that works with the Codename One MapComponent class to provide support for offline maps.

License

LGPL

Features

  • 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 common MBTiles format into a CN1Tiles file that can be used with this module.

Dependencies

  1. CN1JTar Library

Installation

  1. Install the CN1JTar library into your project.
  2. Copy the CN1OfflineMaps.cn1lib file into the lib directory of your codename one project.
  3. Right click on your project in the project explorer (if using Netbeans) and click "Refresh Libs".

Code Example

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();

How to Create an Offline Map

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:

  1. TileMill - A Desktop application that allows you to create maps using public data sources like Open Street Maps.
  2. CN1TileConverter - A Desktop utility that converts mbtiles files to cn1tiles 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).