×
Namespaces

Variants
Actions
(Difference between revisions)

How to create custom Map View in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
jasfox (Talk | contribs)
m (Jasfox - add monospacing.)
trashedDev (Talk | contribs)
(TrashedDev -)
Line 1: Line 1:
[[Category:Java ME]][[Category:Code Examples]][[Category:Symbian]][[Category:Series 40]][[Category:How To]][[Category:Nokia Maps]]
+
[[Category:Java ME]][[Category:Code Examples]][[Category:Symbian]][[Category:Series 40]][[Category:How To]][[Category:Nokia Maps]][[Category:Series 40 6th Edition FP1]][[Category:Series 40 Developer Platform 2.0]][[Category:Java Runtime]]
 
This article explains how to retrieve the available map views and apply one of them to your custom Maps by using [http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/ 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.
 
This article explains how to retrieve the available map views and apply one of them to your custom Maps by using [http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/ 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.
 
{| class="wikitable"
 
{| class="wikitable"
Line 170: Line 170:
 
|sourcecode=[[Media:mapsCustomViewSource.zip]]
 
|sourcecode=[[Media:mapsCustomViewSource.zip]]
 
|installfile=[[Media:mapsCustomViewBinaries.zip]]
 
|installfile=[[Media:mapsCustomViewBinaries.zip]]
|devices=X3-02
+
|devices=X3-02, Asha 305
|sdk=[http://www.developer.nokia.com/Develop/Java/ Nokia SDK 1.0 for Java]
+
|sdk=[http://www.developer.nokia.com/Develop/Java/ Nokia SDK 1.0 for Java], [http://www.developer.nokia.com/Develop/Java/ Nokia SDK 2.0 for Java (beta)]
 
|platform=Series 40, Symbian
 
|platform=Series 40, Symbian
 
|devicecompatability=All
 
|devicecompatability=All

Revision as of 11:37, 28 June 2012

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.

Normal View Satellite View Hybrid View
Normalview.png Satelliteview.png Hybridview.png
Ordinary map view with large labels for mobile Aerial photography image no labels Aerial photography image with roads and labels
Terrain View Transit View Night View
Terrainview.png Transitview.png Nightview.png
Topographical Map Map highlighting Public Transport Low glare map for night use.

Contents

Introduction

Nokia 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, allows to get information about the view and eventually apply it, by calling the setBaseMapType() method.

Integration of Nokia Maps API for Java ME into the working project

Currently the version 1.0, Nokia 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 ExampleTested with
Devices(s): X3-02, Asha 305
Compatibility
Device(s): All
Dependencies: Nokia Maps API for Java ME v1.0
Article
Keywords: Hybrid view, Satellite view, Normal view, Terrain view, Maps, Nokia Maps
Created: skalogir (08 Nov 2011)
Reviewed: skalogir (08 Nov 2011)
Last edited: trashedDev (28 Jun 2012)
331 page views in the last 30 days.
×