How to create custom Map View in Java ME
This article explains how to retrieve the available map views and apply one of them to your custom Maps by using Map API for Java ME. This makes it possible to display a location by using aerial satellite images. Hybrid, terrain and several other modes are also available.
Contents |
Introduction
HERE Maps API for Java ME uses the MapProvider class in order to display the available view modes. MapProvider is a base class used to identify a provider for layered map information. Map providers are divided into those providing data for a base map and those providing data for a transparent overlay layer that can be added on top of the base map (e.g. traffic overlay). Satellite, hybrid, terrain and normal map displays are regarded as the base map on top of which, one can add overlays. Each map mode (hybrid, satellite, normal, terrain) corresponds to one MapProvider. The MapDisplay class, supports retrieval of the available base maps, by calling the getAvailableBaseMap() method. This returns an array of MapProvider objects. Iterating through the items of the array, makes it possible to get information about the view and eventually apply it, by calling the setBaseMapType() method.
Integration of HERE Maps API for Java ME into the working project
Currently the version 1.0, HERE Maps API for Java ME is provided as a separate library. For details, including documentation and the downloadable .jar file, check the "see also" link.
Implementing the mapsCustomView midlet
import com.nokia.maps.common.ApplicationContext;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* Minimal MIDP application to show map content to the user.
*/
public class MapTypeMIDlet extends MIDlet {
protected void startApp() throws MIDletStateChangeException {
// Insert your own AppId and Token, as obtained from the above
// URL into the two methods below.
ApplicationContext.getInstance().setAppID("...");
ApplicationContext.getInstance().setToken("...");
Display display = Display.getDisplay(this);
MapTypeDemo minimalMap = new MapTypeDemo(display, this);
display.setCurrent(minimalMap);
}
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {}
protected void pauseApp() {}
}
import com.nokia.maps.common.GeoCoordinate;
import com.nokia.maps.map.MapCanvas;
import com.nokia.maps.map.MapDisplay;
import com.nokia.maps.map.MapDisplayState;
import com.nokia.maps.map.MapProvider;
import com.nokia.maps.map.MapSchemeListener;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
/**
* This demonstrates Map Type usage, allowing a user to view the available map types.
*
*/
public class MapTypeDemo extends MapCanvas implements MapSchemeListener, CommandListener {
private MapProvider[] mapSchemes;
private Command[] options;
private final Command EXIT = new Command("Exit", Command.EXIT, 1);
protected MIDlet midlet; // for notifyDestroyed
public MapTypeDemo(Display display, MIDlet midlet) {
super(display);
this.midlet = midlet;
init();
}
private void init() {
addCommand(EXIT);
setCommandListener(this);
map.setState(
new MapDisplayState(new GeoCoordinate(51.477, 0.0, 0), 14));
map.getAvailableBaseMaps(this);
}
/**
* Called when the list of available map schemes has been downloaded from
* the network. This indicates that online maps are available and that the
* list of schemes returned from {@link MapDisplay#getAvailableMapOverlays() } is
* complete.
*/
public void onMapSchemesAvailable(MapProvider[] mapSchemes) {
this.mapSchemes = mapSchemes;
setTitle(map.getBaseMap().getName());
options = new Command[mapSchemes.length];
for (int i = 0; i < mapSchemes.length; i++) {
String thisoption = mapSchemes[i].getName();
options[i] = new Command(thisoption, Command.HELP, 5);
addCommand(options[i]);
}
display.setCurrent(this);
}
private void showError(Throwable reason) {
Alert a = new Alert("ERROR", reason.toString(), null, AlertType.INFO);
a.setTimeout(Alert.FOREVER);
display.setCurrent(a, this);
}
public void commandAction(final Command c, Displayable d) {
if (c == EXIT) {
midlet.notifyDestroyed();
} else {
for (int i = 0; i < options.length; i++) {
if (c == options[i]) {
try {
map.setBaseMapType(mapSchemes[i]);
setTitle(map.getBaseMap().getName());
} catch (IllegalArgumentException iae) {
showError(iae);
}
break;
}
}
}
}
public void onMapContentComplete() {
// No code required.
}
public void onMapUpdateError(String description, Throwable detail, boolean critical) {
showError(detail);
}
public void onMapSchemesError(Throwable reason) {
showError(reason);
}
}
Source and Binary Files
The code above can be downloaded from File:MapsCustomViewSource.zip
The installation .jar and .jad files can be downloaded from File:MapsCustomViewBinaries.zip
Article Metadata
Code Example
Tested with
Compatibility
Article


Desupratim - java.lang.NoClassDefFoundError: com.nokia.maps.map.MapCanvas
Hi
I need help i got a error "java.lang.NoClassDefFoundError: com.nokia.maps.map.MapCanvas" I was add Maps_API.jar as External jar.. No Error show in project but when i run this project in eclipes the Nokia SDK given this error...
HOW TO SLOVE THIS???? ITS URGENT....desupratim 10:24, 6 June 2012 (EEST)
Jasfox - Ensure that the Maps_API.jar is exported correctly.
The code will compile if you reference the Maps_API.jar, but since the jar is not a standard option in the SDK it is not available in the SDK by default. You will need to include the Maps_API.jar as part of your binary.
In Eclipse, right click on your project, select properties and under the Java Build Path menu item select Order and Export. Make sure that the Maps_API.jar is ticked as an exported entry.jasfox 13:35, 3 August 2012 (EEST)