How to display an Address or a Point of Interest on a Map with Java ME
This article describes how to search for an address or a point of interest and display it on the Map. It uses Nokia's Map API for Java ME for the translation of the search string into valid coordinates and for the retrieval of the relevant map image.
Contents |
Introduction
Device Considerations
The device requirements for running this example is MIDP2.0 and CLDC 1.1. This covers a very large group of devices from Symbian to Series 40 and from keyboard to Full Touch and Touch and Type. Nokia's Map API for Java ME masks away the differences between various devices and provides different interaction modes for each device, without the developer having to worry about the details of the implementation. With respect to zooming, the following interaction modes are supported:
- On non-touch devices, the zoom functionality is provided by the star (*) and hash (#) keys for zooming in and out respectively. No zoom controls are displayed on the screen.
- On full-touch devices, the zoom functionality is provided by the zoom controls displayed on the screen.
- On Touch and Type devices, the zoom functionality is provided by both the star (*) and hash (#) keys for zooming in and out respectively and the zoom controls displayed on the screen.
With respect to panning (i.e. scrolling or moving the screen around the target location), the following interaction modes are supported:
- On non-touch devices, the navigation keypad is used to move the center of the map up, down, left and right from its current position
- On full-touch and Touch and Type devices, panning is touch driven.
- On full-touch devices with keyboard, such as Nokia E7-00, panning is also supported by the arrow keyboard's arrow keys.
The MIDlet source
import com.nokia.maps.common.GeoCoordinate;
import com.nokia.maps.map.MapCanvas;
import com.nokia.maps.map.MapDisplay;
import com.nokia.maps.map.MapShapeType;
import com.nokia.maps.map.MapStandardMarker;
import com.nokia.maps.search.Location;
import com.nokia.maps.search.SearchManager;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;
public class NokiaMapsShowPOIonMap extends MIDlet implements CommandListener
{
Display display;
MapCanvas mapcanvas;
Form mainform;
MapDisplay mapdisplay;
GeoCoordinate geo;
TextField text=new TextField("Type Address or POI","",100,TextField.ANY);
StringItem infomessage=new StringItem("","");
Command exitCmd=new Command("Exit",Command.EXIT,0);
Command backCmd=new Command("Back",Command.BACK,0);
Command searchCmd=new Command("Search",Command.OK,1);
Location[] locationresults;
MapStandardMarker marker;
public void startApp()
{
//Main Search Screen
display=Display.getDisplay(this);
mainform=new Form("Address or POI");
mainform.append(text);
mainform.append(infomessage);
mainform.addCommand(searchCmd);
mainform.addCommand(exitCmd);
mainform.setCommandListener(this);
display.setCurrent(mainform);
}
public void showOnMap(String poi)
{
try
{
mapcanvas = new MapCanvas(display);
mapdisplay=mapcanvas.getMapDisplay();
//The search Manager for this search
SearchManager searchManager = SearchManager.getInstance();
//Retrieves the Point of Interest
searchManager.geocode(poi, null );
//Stores the results in an array of Location objects
locationresults = searchManager.getLocations();
//The first result is taken
geo=locationresults[0].getDisplayPosition();
//Centers the Map at the Point of Interest
mapdisplay.setCenter(geo);
//Displays the marker
marker = mapcanvas.getMapFactory().createStandardMarker(geo, 48, null,MapShapeType.baloon);
mapdisplay.addMapObject(marker);
//Sets the zoom level
mapdisplay.setZoomLevel(14,0,0);
//Displays the Map on the Screen
mapcanvas.setTitle(poi);
mapcanvas.addCommand(exitCmd);
mapcanvas.addCommand(backCmd);
mapcanvas.setCommandListener(this);
display.setCurrent(mapcanvas);
}catch(RuntimeException e)
{
infomessage.setText("Error:"+e.getMessage());
}
}
public void commandAction(Command c, Displayable d)
{
if(c==searchCmd)
{
String search=text.getString();
infomessage.setText("");
if(!search.equals(""))
{
showOnMap(search);
}
}
if(c==exitCmd)
{
notifyDestroyed();
}
if(c==backCmd)
{
display.setCurrent(mainform);
}
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
}
Resources
The example's source code can be downloaded from here: File:NokiaMapsShowPOIonMapSource.zip
The example's installation binary files can be downloaded from here: File:NokiaMapsShowPOIonMapBinaries.zip
See also
- Nokia's Maps API for Java ME
- Using a JSR-179 Location Provider and displaying the result on a Map
- How to Create a Custom Map View with Nokia's Map API for Java ME
Article Metadata
Code Example
Tested with
Compatibility
Article



