Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 09:28, 20 September 2013 by jasfox (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Implementing Pinch zooming with Gesture API for Maps in Java ME

From Wiki
Jump to: navigation, search
Featured Article
29 Jul

This article explains how to implement own pinch zooming (zoom in and out) support for maps implemented with Maps API in Series 40 Java 2.0 runtime.

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.

Article Metadata
Code ExampleTested with
Devices(s): Nokia Asha 311
Platform(s): Series 40
Series 40
Device(s): Full touch devices
Keywords: Nokia Maps, Java ME, zoom, pinch, touch, gesture
Created: bandarap (07 Feb 2012)
Last edited: jasfox (20 Sep 2013)




The Maps API for Java ME makes it possible to embed Nokia maps into Java ME applications. However the Maps API is missing pinch zoom functionality. If Java ME applications want to support pinch zoom functionality in maps then they have to implement custom pinch zooming in their application. Note that this custom pinch zooming works only on Series 40 full touch devices and will not work on other devices.

Enabling Pinch zoom

Generally there are two ways to implement pinch action, one being MultiPoint touch events and other one is Gesture API. Using Gesture API is the right choice since Gesture API provides ready-made pinch implementation. Pinch zooming support for maps in Series 40 Java can be implemented using Gesture API . Pinch zooming happens in Series 40 devices when two fingers are pressed on screen and one finger is moved near to other finger (pinch zoom in) or moved away (pinch zoom out).

Gesture event handling

To implement support for pinch zoom in the map, a gesture event of pinch has to be registered and handled inside MapCanvas class, which would result in MapCanvas class looking like the following:

public class MyMapCanvas extends MapCanvas implements GestureListener,… {

private GestureInteractiveZone gestureZone;
public MyMapCanvas(Display display, MIDlet midlet) {

GestureRegistrationManager.setListener(this, this);
// Register for pinch events in the whole canvas area
GestureInteractiveZone gestureZone = new GestureInteractiveZone(
GestureRegistrationManager.register(this, gestureZone);

public void gestureAction(Object arg0, GestureInteractiveZone arg1,
GestureEvent arg2) {

Pinch zooming is actually handled by calculating the difference of the distances between start and end point of moving finger. When pinch is happened, gestureAction(…) method will be called. Check the GestureInteractiveZone.GESTURE_PINCH type and do the calculation for pinch zoom. Distance is calculated based on the difference of distance between current pinch distance (distance between two fingers after pinch) and starting pinch distance (distance between two fingers before pinch). 100 pixels difference is used as one zoom level change, and the max/min zoom value is checked before setting the zoom. Finally the map is set to the original center.

public void gestureAction(Object arg0, GestureInteractiveZone aGestureZone,
GestureEvent aGestureEvent) {
int eventType = aGestureEvent.getType();
switch (eventType) {
case GestureInteractiveZone.GESTURE_PINCH: // Pinch detected
int curPinchDistance = aGestureEvent.getPinchDistanceCurrent();
int startingPinchDistance = aGestureEvent
int zoomNew = zoomOrg
+ (int) ((curPinchDistance - startingPinchDistance) / 100);
if (zoomNew > getMapDisplay().getMaxZoomLevel()) {
zoomNew = (int) getMapDisplay().getMaxZoomLevel();
if (zoomNew < getMapDisplay().getMinZoomLevel()) {
zoomNew = (int) getMapDisplay().getMinZoomLevel();
if (zoomNew != zoomOrg) {
getMapDisplay().setZoomLevel(zoomNew, 0, 0);
zoomOrg = getMapDisplay().getZoomLevel();


Full source codes for the example can be found from File:JavaMeMaps Gesture


The Maps API for Java ME has much more functionality which makes it possible to integrate all Nokia Maps features into Java ME applications.

This page was last modified on 20 September 2013, at 09:28.
63 page views in the last 30 days.