Skip to content

Instantly share code, notes, and snippets.

@shannah
Created March 19, 2014 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shannah/9646300 to your computer and use it in GitHub Desktop.
Save shannah/9646300 to your computer and use it in GitHub Desktop.
Demo application for CN1OfflineMaps
package ca.weblite.offlinemaps.demo;
import ca.weblite.codename1.mapbox.MBTilesProvider;
import com.codename1.io.ConnectionRequest;
import com.codename1.io.Log;
import com.codename1.io.NetworkManager;
import com.codename1.maps.MapComponent;
import com.codename1.ui.Button;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Label;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import java.io.IOException;
import java.io.InputStream;
public class OfflineMapsDemo {
private Form current;
MBTilesProvider provider;
static String mapName = "ocenstest.cn1tiles";
static String mapUrl = "http://downloads.ocens.com/downloads/support/ocenstest.cn1tiles";
public void init(Object context) {
try {
Resources theme = Resources.openLayered("/theme");
UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
} catch (IOException e) {
e.printStackTrace();
}
}
public void start(){
Form hi = new Form("Start");
Button loadbtn = new Button("Load map");
loadbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
startMap();
}
});
hi.addComponent(loadbtn);
hi.show();
}
public void startMap() {
try {
if (MBTilesProvider.isLoaded(mapName)) {
// The map named "output.cn1tiles" is already loaded…
// We can just create a provider for this map.
Log.p("Loaded... just setting the provider");
provider = new MBTilesProvider(mapName);
} else {
Log.p("Not loaded... we will load it from the network");
// 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):
ConnectionRequest conn = new ConnectionRequest(){
@Override
protected void readResponse(InputStream input) throws IOException {
provider = MBTilesProvider.create(mapName, input);
Display.getInstance().callSerially(new Runnable(){
public void run() {
showMap();
}
});
}
};
conn.setUrl(mapUrl);
NetworkManager.getInstance().addToQueue(conn);
//provider = MBTilesProvider.createFromResource("/output.cn1tiles");
}
if ( provider == null ){
Form wait = new Form("Please Wait...");
Label waitingLbl = new Label("Loading map tiles. This may take time.");
wait.addComponent(waitingLbl);
wait.show();
} else {
showMap();
}
} catch (IOException ex) {
Log.e(ex);
}
}
public void showMap(){
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();
}
public void stop() {
current = Display.getInstance().getCurrent();
}
public void destroy() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment