×
Namespaces

Variants
Actions
(Difference between revisions)

How to create custom Map View in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
skalogir (Talk | contribs)
(Skalogir -)
hamishwillee (Talk | contribs)
(28 intermediate revisions by 7 users not shown)
Line 1: Line 1:
[[Category:Draft]][[Category:Java ME]][[Category:Code Examples]][[Category:Symbian]][[Category:Series 40]][[Category:Location]]
+
[[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:Series 40 Developer Platform 1.1]][[Category:Series 40 Developer Platform 2.0]]
{{This article explains how to retrieve the available map views and apply one of them to your custom Maps by using [https://projects.developer.nokia.com/LBSPJME 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, terrain and several other modes are also available.
 
+
{| class="wikitable"
 +
|-
 +
! Normal View !! Satellite View !! Hybrid View
 +
|-
 +
| [[File:normalview.png|250px]] || [[File:satelliteview.png|250px]]|| [[File:hybridview.png|250px]]
 +
|-
 +
|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
 +
|-
 +
| [[File:terrainview.png|250px]] || [[File:transitview.png|250px]] || [[File:nightview.png|250px]]
 +
|-
 +
| Topographical Map  || Map highlighting Public Transport || Low glare map for night use.
 +
|}
 
== Introduction ==
 
== 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.  
+
Nokia Maps API for Java ME uses the {{Icode|MapProvider}} class in order to display the available view modes. {{Icode|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 {{Icode|MapProvider}}. The {{Icode|MapDisplay}} class, supports retrieval of the available base maps, by calling the {{Icode|getAvailableBaseMap()}} method. This returns an array of {{Icode|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 {{Icode|setBaseMapType()}} method.
  
== Integration of Nokia Maps API for Java ME into the 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 provided 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 downloadable .jar file, check the "see also" link.
  
 
== Implementing the mapsCustomView midlet ==
 
== Implementing the mapsCustomView midlet ==
  
 
<code java>
 
<code java>
 +
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() {}
 +
}
 +
</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.
 +
*
 +
*/
 +
public class MapTypeDemo extends MapCanvas implements MapSchemeListener, CommandListener {
  
Display display;   
+
    private MapProvider[] mapSchemes;
MapCanvas mapcanvas;   
+
    private Command[] options;
MapDisplay mapdisplay;   
+
     private final Command EXIT = new Command("Exit", Command.EXIT, 1);
MapProvider[] providers;  
+
    protected MIDlet midlet; // for notifyDestroyed
MapFactory mapfactory;   
+
ChoiceGroup allBaseMaps;   
+
Command parentCommand;   
+
Command[] options;      
+
public void startApp()      
+
{       
+
display=Display.getDisplay(this);       
+
mapcanvas = new MapCanvas(display);       
+
mapdisplay=mapcanvas.getMapDisplay();             
+
providers=mapdisplay.getAvailableBaseMap();             
+
options=new Command[providers.length];             
+
for(int i=0;i<providers.length;i++)       
+
{                       
+
String thisoption=providers[i].getName();           
+
options[i]=new Command(thisoption,Command.HELP,5);          
+
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)
+
{       
+
for(int i=0;i<options.length;i++)       
+
{           
+
if(c==options[i])           
+
{               
+
mapdisplay.setBaseMapType(providers[i]);         
+
}       
+
}           
+
}
+
  
}
+
    public MapTypeDemo(Display display, MIDlet midlet) {
</code>
+
        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);
 +
    }
 +
}</code>
 +
 
 +
== 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]]
  
== See also ==
+
{{SeeAlso|
[https://projects.developer.nokia.com/LBSPJME Nokia Maps API for Java ME]
+
[http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/ Map API for Java ME]
 +
* [[Implementing a custom MapUrlProvider overlay with Maps API for Java ME|Creating a custom MapUrlProvider overlay]]
 +
* [[Setting the Map language in the Maps API for Java ME|Setting the Map language]]
 +
* [[Creating a Map Type Selector for the Maps API for Java ME|Creating a Map Type Selector]]
 +
}}
  
  
 
{{ArticleMetaData
 
{{ArticleMetaData
  
|sourcecode=[[Media:MapCustomItem.java.zip]]
+
|sourcecode=[[Media:mapsCustomViewSource.zip]]
[[Media:MapFormMIDlet.java.zip]]
+
|installfile=[[Media:mapsCustomViewBinaries.zip]]
|installfile=[[Media:JavaMELocationAPI_MapCustomItem.zip]]
+
|devices=X3-02, Asha 305
|devices=X3-02
+
|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)]
|sdk=[http://www.developer.nokia.com/Develop/Java/ Nokia SDK 1.0 for Java]
+
|platform=Series 40, Symbian
|platform=S40, Symbian^1, Symbian^3
+
 
|devicecompatability=All
 
|devicecompatability=All
 
|signing=
 
|signing=
|dependencies=Nokia Maps API for Java ME v0.5
+
|dependencies=Nokia Maps API for Java ME v1.1
 
|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=Location API, Ovi Maps, Form, CustomItem, Gauge
+
|keywords=Hybrid view, Satellite view, Normal view, Terrain view, Maps, Nokia Maps, Java ME, custom map, view
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|id= <!-- Article Id (Knowledge base articles only) -->
|creationdate=20110628
+
|creationdate=20111108
|author=[[User:jappit]]
+
|author=[[User:skalogir]]
 
|review-by=[[User:skalogir]]
 
|review-by=[[User:skalogir]]
|review-timestamp=20111006
+
|review-timestamp=20111108
 
}}
 
}}

Revision as of 05:29, 7 August 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, makes it possible 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.1
Article
Keywords: Hybrid view, Satellite view, Normal view, Terrain view, Maps, Nokia Maps, Java ME, custom map, view
Created: skalogir (08 Nov 2011)
Reviewed: skalogir (08 Nov 2011)
Last edited: hamishwillee (07 Aug 2012)
193 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×