×
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 - Corrected Category)
jasfox (Talk | contribs)
(Jasfox - Update to reflect version 1.0)
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]]
This article explains how to retrieve the available map views and apply one of them to your custom Maps by using [http://projects.developer.nokia.com/LBSPJME/wiki Nokia Maps API for Java ME]. This makes it possible to display a location by using aerial satellite images. Hybrid and terrain 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 and terrain modes are also available.
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
Line 17: Line 17:
 
== Integration of Nokia Maps API for Java ME into the working project ==
 
== Integration of Nokia Maps API for Java ME into the working project ==
  
As of version 0.5, Nokia Maps API for Java ME is provided as a separate library. The link at the bottom of this page, redirects to the location where the API, including documentation and the .jar file, can be downloaded.
+
Currently the version 1.0, Nokia Maps API for Java ME is provided as a separate library. For details, including documentation and the downlaodable .jar file, check the "see also" link.
  
 
== Implementing the mapsCustomView midlet ==
 
== Implementing the mapsCustomView midlet ==
  
 
<code java>
 
<code java>
 +
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() {}
 +
}
 +
</code>
 +
<code java>
 +
import com.nokia.maps.common.GeoCoordinate;
 
import com.nokia.maps.map.MapCanvas;
 
import com.nokia.maps.map.MapCanvas;
 
import com.nokia.maps.map.MapDisplay;
 
import com.nokia.maps.map.MapDisplay;
import com.nokia.maps.map.MapFactory;
+
import com.nokia.maps.map.MapDisplayState;
 
import com.nokia.maps.map.MapProvider;
 
import com.nokia.maps.map.MapProvider;
import javax.microedition.lcdui.ChoiceGroup;
+
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.Command;
 
import javax.microedition.lcdui.CommandListener;
 
import javax.microedition.lcdui.CommandListener;
 
import javax.microedition.lcdui.Display;
 
import javax.microedition.lcdui.Display;
 
import javax.microedition.lcdui.Displayable;
 
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.*;
+
import javax.microedition.midlet.MIDlet;
  
public class mapsCustomView extends MIDlet implements CommandListener
+
/**
{  
+
* This demonstrates Map Type usage, allowing a user to view the available map types.
  Display display;   
+
*
  MapCanvas mapcanvas;   
+
*/
  MapDisplay mapdisplay;   
+
public class MapTypeDemo extends MapCanvas implements MapSchemeListener, CommandListener {
  MapProvider[] providers;   
+
  MapFactory mapfactory;   
+
  ChoiceGroup allBaseMaps;   
+
  Command parentCommand;   
+
  Command[] options;       
+
  
  public void startApp()       
+
    private MapProvider[] mapSchemes;
  {       
+
    private Command[] options;
      display=Display.getDisplay(this);      
+
    private final Command EXIT = new Command("Exit", Command.EXIT, 1);
      mapcanvas = new MapCanvas(display);       
+
    protected MIDlet midlet; // for notifyDestroyed
      mapdisplay=mapcanvas.getMapDisplay();              
+
      providers=mapdisplay.getAvailableBaseMap();             
+
      options=new Command[providers.length];              
+
  
      for(int i=0;i<providers.length;i++)      
+
    public MapTypeDemo(Display display, MIDlet midlet) {
      {                      
+
        super(display);
        String thisoption=providers[i].getName();          
+
        this.midlet = midlet;
        options[i]=new Command(thisoption,Command.HELP,5);          
+
        init();
        mapcanvas.addCommand(options[i]);       }               
+
    }
        display.setCurrent(mapcanvas);       
+
        mapcanvas.setCommandListener(this);   
+
      }       
+
      public void pauseApp()
+
    {
+
+
    }
+
       
+
  public void destroyApp(boolean unconditional) {    }  
+
  
   public void commandAction(Command c, Displayable arg1)  
+
    private void init() {
  {        
+
 
      for(int i=0;i<options.length;i++)      
+
        addCommand(EXIT);
      {            
+
        setCommandListener(this);
        if(c==options[i])          
+
        map.setState(
        {              
+
                new MapDisplayState(new GeoCoordinate(51.477, 0.0, 0), 14));
            mapdisplay.setBaseMapType(providers[i]);          
+
        map.getAvailableBaseMaps(this);
        }      
+
    }
      }          
+
 
  }
+
    /**
}
+
    * Called when the list of available map schemes has been downloaded from
</code>
+
    * 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);
 +
    }
 +
}</code>
  
 
== Source and Binary Files ==
 
== Source and Binary Files ==
Line 85: Line 154:
 
The installation .jar and .jad files can be downloaded from [[File:mapsCustomViewBinaries.zip]]
 
The installation .jar and .jad files can be downloaded from [[File:mapsCustomViewBinaries.zip]]
  
== See also ==
+
{{SeeAlso|
[http://projects.developer.nokia.com/LBSPJME/wiki Nokia Maps API for Java ME]
+
[http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/ Map API for Java ME]
 +
}}
  
  
Line 98: Line 168:
 
|devicecompatability=All
 
|devicecompatability=All
 
|signing=
 
|signing=
|dependencies=Nokia Maps API for Java ME v0.5
+
|dependencies=Nokia Maps API for Java ME v1.0
 
|capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|keywords=Hybrid view, Satellite view, Normal view, Terrain view, Maps, Nokia Maps
 
|keywords=Hybrid view, Satellite view, Normal view, Terrain view, Maps, Nokia Maps

Revision as of 16:29, 7 March 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 and terrain modes are also available.

Normal View Satellite View
Normalview.png Satelliteview.png
Hybrid View Terrain View
Hybridview.png Terrainview.png

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 downlaodable .jar file, check the "see also" link.

Implementing the mapsCustomView midlet

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
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: jasfox (07 Mar 2012)
216 page views in the last 30 days.