×
Namespaces

Variants
Actions
(Difference between revisions)

Adding Markers to the Map with Maps API for Java ME

From Nokia Developer Wiki
Jump to: navigation, search
symbianyucca (Talk | contribs)
(Symbianyucca -)
avnee.nathani (Talk | contribs)
(Avnee.nathani - adding metadata, fix link)
Line 1: Line 1:
 
[[Category:Java ME]][[Category:Nokia Maps]][[Category:Location]][[Category:Code Examples]]
 
[[Category:Java ME]][[Category:Nokia Maps]][[Category:Location]][[Category:Code Examples]]
 
{{Abstract| This article explains how to add markers into the map with [http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/ Maps API for Java ME] API and also how to make them draggable.}}
 
{{Abstract| This article explains how to add markers into the map with [http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/ Maps API for Java ME] API and also how to make them draggable.}}
 +
 +
{{ArticleMetaData
 +
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|platform= Java ME
 +
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 +
|dependencies= Maps API for Java ME
 +
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 +
|id= <!-- Article Id (Knowledge base articles only) -->
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by=<!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by=<!-- After significant update -->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate=20120227
 +
|author=[[User:symbianyucca]]
 +
}}
  
 
== Introduction ==
 
== Introduction ==
  
 
[[File:MapsMarker.png]]
 
[[File:MapsMarker.png]]
The [[http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/ Maps API for Java ME] allows to easily embed Nokia Maps service into Java ME applications. The API also includes functionality on adding markers into the map.
+
The [http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/ Maps API for Java ME] allows to easily embed Nokia Maps service into Java ME applications. The API also includes functionality on adding markers into the map.
  
 
== Constructing and adding markers ==
 
== Constructing and adding markers ==

Revision as of 17:43, 29 February 2012

This article explains how to add markers into the map with Maps API for Java ME API and also how to make them draggable.

Article Metadata
Compatibility
Dependencies: Maps API for Java ME
Article
Created: symbianyucca (27 Feb 2012)
Last edited: avnee.nathani (29 Feb 2012)

Contents

Introduction

MapsMarker.png The Maps API for Java ME allows to easily embed Nokia Maps service into Java ME applications. The API also includes functionality on adding markers into the map.

Constructing and adding markers

Markers are added to the Map by adding them into the instance of MapDisplay object, which can be retrieved from current MapCanvas object. Note that you should never try constructing the marker directly, instead it should be constructed utilizing functions provided by the MapFactory. An instance of the MapFactory can be retrieved with the MapCanvas object, and then createMapMarker method can be used for constructing marker:

MapFactory mapFactory = mapCanvas.getMapFactory();
MapMarker marker = mapFactory.createMapMarker(position, Image.createImage("/nokiaicon.png"));
mapCanvas.getMapDisplay().addMapObject(marker);

You can use custom images with createMapMarker method as shown above, or if you want, you could also use following predefined shapes:

  • baloon
  • hexagon
  • house
  • octagon
  • pentagon
  • rectangle
  • squere
  • star
  • triangle

When using the shapes, you can also define the size in pixels (as second argument) as well as the text (as third argument) to be used inside the marker as illustrated below:

MapMarker marker = mapFactory.createMapMarker(position,40, "3",MapShapeType.squere);

Implementing Draggable markers

As shown in Handling map events with Maps API for Java ME article, if you want to catch map pointer events, you need to implement custom MapComponent with EventListener. Thus with draggable markers you need to implement these components as well, and with this example all logic for dragging is also implemented inside the EventListener component. First thing to do with the pointer events is then to catch the event where the marker is pressed before the dragging is started. This of naturally is handled inside the pointerPressed method:

public boolean pointerPressed(int x, int y) {
if(mapDisplay != null){
nowPoint = new Point(x,y);
MapObject selObj = mapDisplay.getObjectAt(nowPoint);
if (selObj != null && selObj instanceof MapStandardMarker) {
marker = (MapStandardMarker)selObj;
clicking = true;
movingMarker = false;
return true;
}else {
return false;
}
} else {
return false;
}
}

Basically with the code getObjectAt method is used for determining whether there is an marker under the point pressed, and if there is, then dragging is started. Then the next step is to actually drag the marked when user moves the pointer in the map.

public boolean pointerDragged(int x, int y) {
if(marker != null){
clicking = false;
movingMarker = true;
marker.setCoordinate(mapDisplay.pixelToGeo(new Point(x, y)));
mapCanvas.repaint();
return true;
}else{
movingMarker = clicking = false;
return false;
}
}

Note that if there is no marker selected in the pointerPressed method, then this function need to return false, otherwise the normal panning with the map would also be disabled. And once the user stops the dragging and lifts the finger from the map, then the pointerReleased method is called. In this method we need to finalize the dragging, also with this example there is separate handling for clicking the marker:

public boolean pointerReleased(int x, int y) {
boolean retVal = false;
if(marker != null){
if(clicking){
mapDisplay.setCenter(marker.getCoordinate());
mapCanvas.repaint();
retVal = true;
}else if(movingMarker){
marker.setCoordinate(mapDisplay.pixelToGeo(new Point(x, y)));
mapDisplay.setCenter(marker.getCoordinate());
mapCanvas.repaint();
retVal = true;
}
 
}
 
marker = null;
clicking = movingMarker = false;
return retVal;
}


Resources

Full source code of the MapRevGeoCoding is available here: File:Java MapsMarker.zip

Summary

The Java ME Location API offers rich functionalities that allows to integrate all the main Ovi Maps features in a Java ME application, with just a few lines of code.

380 page views in the last 30 days.
×