×
Namespaces

Variants
Actions
(Difference between revisions)

How to display an Address or a Point of Interest on a Map with Java ME

From Nokia Developer Wiki
Jump to: navigation, search
skalogir (Talk | contribs)
(Skalogir -)
skalogir (Talk | contribs)
(Skalogir -)
Line 14: Line 14:
 
| '''Nokia-MIDlet-On-Screen-Keypad: no'''
 
| '''Nokia-MIDlet-On-Screen-Keypad: no'''
 
|}
 
|}
 +
==The MIDlet source==
 +
<code java>
 +
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) {}   
 +
}
 +
</code>
 
==Resources==
 
==Resources==
  

Revision as of 16:37, 13 January 2012

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.

Sydneypoi.pngSydneyshow.png

Contents

Introduction

Disabling the On Screen Keypad

In order to take advantage of the entire screen, the following line should be added to the application's file descriptor (jad file):

Nokia-MIDlet-On-Screen-Keypad: no

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

Article Metadata
Code ExampleTested with
Devices(s): Nokia X3-02, Nokia 6210N, Nokia 6700classic, Nokia 701
Compatibility
Device(s): MIDP2.0/CLDC 1.1
Article
Keywords: Nokia Maps API for Java ME, Image, Coordinates, Search Address or Point of Interest on a Map, GeoLocation
Created: skalogir (13 Jan 2012)
Reviewed: skalogir (13 Jan 2012)
Last edited: skalogir (13 Jan 2012)
192 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.

×