OStreetMap

OpenStreetMap is a free editable map of the whole world. OStreetMap is a Java library for Java ME that renders OpenStreetMap on Java ME enabled phones. Idea is that OStreetMap does all the basic work of fetching and drawing map tiles on the screen. User provides map coordinates and zoom level for rendering and rest is handled automatically. Currently fetched map tiles are not stored permanently but fetched again each time you start your application again.

Library was derived from previous application NavLoc, which was written lying back on sofa while waiting broken angle to heal.

New

24.6.2010
Just added support for touchscreen phones. Now you can pan by dragging finger on screen. There is also zoom-controlson the right edge of screen. This was tested on Nokia 5230

Licence

You can use library freely for personal use but if you consider commercial use contact us for additional licencing and pricing information.

OSMap

OSMap is simplest MIDlet possible demonstrating use of OStreetMap library. It specifies location coordinates and then gives user ability to pan map and change zoom level. Default coordinates are for city of Helsinki in Finland.

For the impatient, here is a precompiled version of OSMap.jad and for those that are not using Mobile for browsing OSMap.jar for actual application archive. If you are browsing with a device that can run Java ME applications, just clicking on jad-file link should automaticly download and install jar-file also.

OSMap.java

This is almost the minimum MIDlet possible. Only lines 14, 18 and 19 are in addition to minimum to instaciate Map-class which extends OStreetMap-class. I could have combined lines 14 and 18 on line 18 making it even smaller but this way it is cleaner in a long run.
If we didn't have those three additional code lines OSMap wouldn't have any user interface!

So the code creates a new Map-class and then makes it to be current Display.

 1.    import javax.microedition.lcdui.Display;
 2.    import javax.microedition.midlet.MIDlet;
 3.    import javax.microedition.midlet.MIDletStateChangeException;
 4.
 5.    /*
 6.     * OSMap.java
 7.     *
 8.     * Created on 27. May 2010
 9.     * @author Okko
10.     */
11.
12.    public class OSMap extends MIDlet
13.    {
14.        private Map map = null;
15.
16.        public OSMap()
17.        {
18.            map = new Map();
19.            Display.getDisplay( this ).setCurrent( map );
20.        }
21.
22.        protected void startApp() throws MIDletStateChangeException {}
23.
24.        protected void pauseApp() {}
25.
26.        protected void destroyApp( boolean unconditional ) {}
27.    }

Map.java

Methods public Map() and empty method public void pt( Graphics g ) is all you need to get OStreetMap to fetch and render map pages. public synchronized void keyPressed(int keyCode) is just to have some user input also for panning and zooming around the map.

Have fun with this small demo program and adding features to it. public void pt( Graphics g ) is called at the end of painting map. There you can draw your own stuff onto the display.

 1.    import javax.microedition.lcdui.Graphics;
 2.
 3.    /* Extend OStreetMap which is based on Canvas.
 4.     *
 5.     * This gives us keyPressed so that we can make an interactive application.
 6.     * Application itself just makes current position to be in Helsinki
 7.     * Finland and sets URL for Tile retrieval.
 8.     */
 9.
10.    /**
11.     *
12.     * @author okko
13.     */
14.
15.    public class Map extends OStreetMap
16.    {
17.
18.        public Map()
19.        {
20.            Coord pos = new Coord();
21.
22.            pos.lat = 60.2034073;
23.            pos.lon = 24.6658730;
24.
25.            url( "http://tile.openstreetmap.org/" );
26.            setOrigo( pos );
27.        }
28.
29.        public void pt( Graphics g )
30.        {
31.        }
32.
33.        /** Process keypresses.
34.         *
35.         * @param keyCode
36.         * Raw keycode from user.
37.         *
38.         * Read keys, 2, 4, 6 and 8 move map a little. 1 and 3 change Zoom level.
39.         */
40.        public synchronized void keyPressed(int keyCode)
41.        {
42.            int gk = getGameAction( keyCode);
43.
44.            if( keyCode == KEY_NUM1 )
45.            {
46.                setZoom( -1 );
47.            }
48.            else if( keyCode == KEY_NUM3 )
49.            {
50.                setZoom( 1 );
51.            }
52.            else if( gk == UP )
53.            {
54.                move( 0.0, -0.1 );
55.            }
56.            else if( gk == DOWN )
57.            {
58.                move( 0.0, 0.1 );
59.            }
60.            else if( gk == LEFT )
61.            {
62.                move( -0.1, 0.0 );
63.            }
64.            else if( gk == RIGHT )
65.            {
66.                move( 0.1, 0.0 );
67.            }
68.
69.        }
70.    }

OSMap.zip

Here is an archive OSMap.zip containing previous code and it includes OStreetMap.jar library which you can also download from latter link.

Later I'll add some other simple demos about using GPS to show your current location and moving map display. Also howto draw stuff onto map according to coordinates instead of display geometry

I'm available at ( juha dot okkonen at greencurrent dot com ) if you want to EMail about this stuff.