Top Banner
CloudMade Maps Developers Community Downloads Blog Help Sign In CloudMade Projects Blog J2ME Lib Examples http://developers.cloudmade.com/projects/j2me-lib/examples/ control-keys Simple Map Lets get going. The next few steps will show you how to build a "Hello world" type of J2ME interactive mapping application called Hello map. We assume that you have installed Sun WTK 2.5.2 , Eclipse IDE (we use 3.3.2) with compatible EclipseME plug-in. 1. Create new Midlet Suite project: in Eclipse, select File > New project, J2ME Midlet Suite. Let's call it hello_map. This creates necessary files and directories, also links to J2ME standard libraries 2. Create very minimal Midlet code. First create a package, call it com.tutorial. Into the package add a new file called
20
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

CloudMade Maps Developers Community Downloads Blog Help Sign In CloudMade Projects Blog

J2ME Lib Exampleshttp://developers.cloudmade.com/projects/j2me-lib/examples/control-keys

Simple MapLets get going. The next few steps will show you how to build a "Hello world" type of J2ME interactive mapping application called Hello map. We assume that you have installed Sun WTK 2.5.2 , Eclipse IDE (we use 3.3.2) with compatible EclipseME plug-in. 1. Create new Midlet Suite project: in Eclipse, select File > New project, J2ME Midlet Suite. Let's call it hello_map. This creates necessary files and directories, also links to J2ME standard libraries 2. Create very minimal Midlet code. First create a package, call it com.tutorial. Into the package add a new file called HelloMap.java. You can copy-paste the following code into the package:3. 4. 5. 6. 7. package com.tutorial; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class HelloMap

8. extends MIDlet 9. implements CommandListener { 10. private Form mMainForm; 11. public HelloMap() { 12. mMainForm = new Form("Hello map"); 13. mMainForm.append(new StringItem(null, "Hello, map!\n")); 14. mMainForm.addCommand(new Command("Exit", Command.EXIT, 0)); 15. mMainForm.setCommandListener(this); 16. } 17. 18. public void startApp() { 19. Display.getDisplay(this).setCurrent(mMainForm); 20. } 21. 22. public void pauseApp() {} 23. 24. public void destroyApp(boolean unconditional) {} 25. 26. public void commandAction(Command c, Displayable s) { 27. notifyDestroyed(); 28. } }

29. Now configure the JAD file. Open hello_map.jad file, which should be found in the root directory of your project. It should be opened by default in graphical Application Description editor (of course this is for sheep and brave ones will use plain text editor like vi to do this ;-)). The only mandatory thing to be defined here is the Midlet. Open the second sheet in the editor called Midlets, click the button Add and modify the contents of a new row: Name: HelloMap and select the cell Class. Now the selector icon should appear in the right part of the cell, click on this. This opens "Choose Midlet" dialog with a pre-filled * character. Delete it and start to type He, magically HelloMap appears as the right choice. Select it with doubleclick. Now the midlet class com.tutorial.HelloMap should be defined. Save the JAD file. 30. Now's a good time to check that our app compiles properly. Right-click on project, select J2ME > Create Package. The JAR and JAD file should be now generated into build directory. 31. Run your Midlet in the emulator: right-click project, select Run As > Open Run Dialog. Select Wireless Toolkit Emulator and click "New" to create a new configuration. As "Executable" it is better to select "Midlet" (instead of Over the Air), and as "Midlet" select com.tutorial.HelloMap. Now click "Run". The WTK phone emulator window should be opened with application running, showing the text "Hello, map!". So far so good, but there is no map yet. We'll add it with the next steps. 32. Add MGMaps Library JAR to the project. Copy the maps_lib-xxx.jar file to project's directory, right-click on the file, and select Build Path > Add to Build Path. Eclipse should now show the library JAR in "Referenced Libraries" group in the project explorer window. One more click is needed here to ensure that the necessary mapping JAR code will be also in your application JAR file: right-click project, select Build Path > Configure Build Path; now select the tab "Order and Export"; here maps_lib-xxx.jar should be listed, selected it and click OK.

33. Now modify your HelloMap.java to have the few extra lines needed for the actual map. The new lines are highlighted below:34. package com.tutorial; 35. import javax.microedition.lcdui.*; 36. import javax.microedition.midlet.*; 37. 38. import com.nutiteq.MapItem; 39. import com.nutiteq.maps.*; 40. import com.nutiteq.components.WgsPoint; 41. import com.nutiteq.controls.ControlKeys; 42. 43. public class HelloMap 44. extends MIDlet 45. implements CommandListener { 46. private Form mMainForm; 47. private MapItem mapItem; 48. 49. public HelloMap() { 50. mapItem = new MapItem("Map", "tutorial", this, 300, 150, new WgsPoint(24.764580, 59.437420), 12); 51. 52. mapItem.setMap(new CloudMade("LICENSE-KEY-REPLACEME",64,1)); 53. 54. mMainForm = new Form("Hello map"); 55. mMainForm.append(new StringItem(null, "Hello, map!\n")); 56. mMainForm.append(mapItem); 57. 58. mMainForm.addCommand(new Command("Exit", Command.EXIT, 0)); 59. mMainForm.setCommandListener(this); 60. mapItem.startMapping(); 61. } 62. 63. public void startApp() { 64. Display.getDisplay(this).setCurrent(mMainForm); 65. mapItem.startMapping(); 66. } 67. 68. public void pauseApp() {} 69. 70. public void destroyApp(boolean unconditional) {} 71. 72. public void commandAction(Command c, Displayable s) { 73. notifyDestroyed(); 74. } }

Let's take a brief look at the code we added:o o o

First we have imports, references to mapping library classes which are used Then we define the mapItem local variable Then we create the MapItem (which is a Custom Form element). The parameters are:

1. Name of Form element (shown as text label) 2. Nutiteq license key. "tutorial" works if you use the default Vendor and Application names like in the tutorial, but for your own application you should generate own key at the Nutiteq.com homepage (Developers section). 3. Reference to the midlet object (this) 4. Maximum size of element (300x150 pixels). Depending on the real device the form element may be smaller (but may not smaller than 100x100 pixels). Typical mobiles will limit the map image width to be more or less equal to the screen width, so I put 300 here just to ensure that it will be "full screen width" (for most devices), the actual width would be 232 for the WTK default emulator, for example. You can also define width=1000 to be sure that it really fills every possible screen (but then some phone may also surprise you with horizontal scrolling capability). The height of the Forms is not limited by the screen, so a typical phone will show vertical scrolling bar if the map is too tall. With additional code it is also possible to find out actual size of screen (Canvas), but this is not covered here. 5. Initial map center location (24.764580 E, 59.437420 N) 6. Initial zoom (12). Zoom = 0 means whole world and maximum zoom for CloudMade is 20 (18 if 256-pixel tiles are used). o We define a custom map server to be used. By default it uses OpenStreetmap (tile.openstreetmap.org) server with 256-pixel tiles. To make use of the CloudMade Mobile Maps API which allows you to use CloudMade's specialised mobile tiles or any of CloudMade's other styles, we can define a custom map class. Parameters for this are: 1. License key for the CloudMade Mobile Maps API. If you don't alread have a key, you can get one here. 2. Size of tiles. Currently CloudMade supports values 64 (64x64 pixel tiles, ideal for mobile devices) and 256 (256x256 pixel tiles, idea for web applications). 3. Style ID. CloudMade supports three different syles - 1 standard web, 2 standard mobile, 3 - nonames. We'll be adding more styles in the future, keep your eyes pinned to our blog for future announcements. o Next we add the mapItem to the screen form. o Finally, after all the preparations, we start mapping process, this initiates the download queue. 75. Now remember rebuild the J2ME package and then run it. You should see the following screen. You can use * and # keys to zoom in/out the map. 76. You can now copy your application to the phone, the installation package is in project's deployed directory. For most phones (all Nokia and SonyEricsson phones) the easiest way is to copy the JAR file to the phone using bluetooth, and then open file and phone will prompt for installation. Some phones may require also JAD file and over-the-air (i.e. over WAP/mobile data) installation.

-

Create a New Sample Project1) File > New > Java Project

2) Create a new Java project Project name. HelloNFC Press Finish.

3) File > New > Package

4) Select MIDlet Project

5) Create a New Midlet Project Project Name: HelloNFC Press Finish

6) Create a new Java Package Package name. at.nfcresear ch Press Finish.

7) File > New > Class

8) Create a new Java Class Package name. HelloNFC Press Finish.

9) Copy & Paste Code form Appendix to Java-File

10) Double Click Application Descriptor

s

11) Select MIDlets, then press ADD and enter HelloNFC as Name and press Browse

12) Choose HelloNFC and confirm the selection with OK.

13) To execute the MIDlet in the Emulator press the Play Button and select Run As and Emulated Java ME MIDlet.

14) Then the 6212 Emulator as well as the NFC Manager Open automaticall y.

15) In order to read the ID of a Tag, just drop on of the predefined tags in the NFC Manager to onto the phone in the NFC Manger and the Emulator will read the UID of the Tag.

16) To check whether the J2ME worked correctly, right click the Tag in the NFC Manager and select Change UID to determine the UID of the tag. Thats it for the beginning. You have successfully tested your fist MIDlet in Nokia 6212 Emulator using

the NFC Manager. http://www.forum.nokia.com/info/sw.nokia.com/id/fb0180d3-5b0b-43e4-8792147488267c2d/ncf_v1_2.zip.html?ticket=ST-83117-gDAluuZdYehqKCBXNIxadKDYfEahh7PKbXS-20 (requires Serial Number) (if you plan to use the Nokia Connectivity Framework, please make sure you download a 1.4.x Version of SDK).Hagenberg Linz Steyr Wels

-9/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package at.nfcresearch; // Packages for contactless Communcation import javax.microedition.contactless.ContactlessException; import javax.microedition.contactless.DiscoveryManager; import javax.microedition.contactless.TargetListener; import javax.microedition.contactless.TargetProperties; import javax.microedition.contactless.TargetType; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; 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.midlet.MIDlet; public class HelloNFC extends MIDlet implements TargetListener, CommandListener { // Display Components // An Exit-Button private Command exitCommand; // A Form to display the information private Form form; public HelloNFC() { // Create the GUI exitCommand = new Command("Exit", Command.EXIT, 1); form = new Form("UIDClient"); form.addCommand(exitCommand); form.append("Touch Tag to read ID."); // add a command Listern, to see when a button is pressed form.setCommandListener(this); // Registration of the TargetListener for external contactless // Targets (in this case RFID_TAG, as it is the most general type). try { DiscoveryManager dm = DiscoveryManager.getInstance(); dm.addTargetListener(this, TargetType.RFID_TAG); // in case the Listener can not be registered, throw an exception } catch (ContactlessException ce) { displayAlert("Unable to register TargetListener: " + ce.toString(), AlertType.ERROR); } } // Method from MIDlet to start the Application public void startApp() { Display.getDisplay(this).setCurrent(form); } // Method from MIDlet to pause the Application public void pauseApp() { } // Method from MIDlet to close the Application public void destroyApp(boolean unconditional) { } /** * Implementation of the Call-Back Function of the TargetListener * @param targetProperties: Array of Targets found by the Phone * */ public void targetDetected(TargetProperties[] targetProperties) { // in case no targets found, exit the method if (targetProperties.length == 0) { return; } // show the UID of the first tag found

Appendix

TargetProperties tmp = targetProperties[0]; displayAlert("UID read: " + tmp.getUid(), AlertType.INFO); // read ID try { form.append("ID read: " + tmp.getUid()); } catch (Exception e) { form.append("Error: " + e.toString()); } } /** * Implementation of the Call-Back function of the CommandListener * @param command: command key pressed * @param displayable: associated displayable Object */ public void commandAction(Command command, Displayable displayable) { if (command == exitCommand) { // Deregister the TargetListener DiscoveryManager dm = DiscoveryManager.getInstance(); dm.removeTargetListener(this, TargetType.NDEF_TAG); destroyApp(false); notifyDestroyed(); } } /** * Helper Function to display a Pop-Up in the Emulator * @param error: Message as Text * @param type: The kind of Pop-up to be displayed */ private void displayAlert(String error, AlertType type) { Alert err = new Alert(form.getTitle(), error, null, type); Display.getDisplay(this).setCurrent(err, form); } }