LWUIT引路蜂地图开发示例:第一个地图应用

jerry 地图开发 2015年11月26日 收藏

依旧采用NetBean作为开发IDE,创建一个LwuitGISEngineTutorial项目,将引路蜂地图开发包libgisengine.jar 和LWUIT开发包 LWUIT.jar复制到lib子目录下,并作为外部Jar库文件添加到项目中。将LWUITTheme.res 和 Licence文件guidebee.lic 放在res 子目录下,并将res目录添加到项目中。

为避免重复,就示例共用的代码设计一个基类,MapDemoLWUIT,并从LWUIT库的Form派生一个子类MapCanvas用来显示地图。

  1. //------------------------------------------------------------------------------
  2. // COPYRIGHT 2011 GUIDEBEE
  3. // ALL RIGHTS RESERVED.
  4. // GUIDEBEE CONFIDENTIAL PROPRIETARY
  5. ///////////////////////////////////// REVISIONS ////////////////////////////////
  6. // Date Name Tracking # Description
  7. // --------- ------------------- ---------- --------------------------
  8. // 11FEB2011 James Shen Initial Creation
  9. ////////////////////////////////////////////////////////////////////////////////
  10. //--------------------------------- PACKAGE ------------------------------------
  11. package com.pstreets.gisengine.demo;
  12.  
  13. //--------------------------------- IMPORTS ------------------------------------
  14. import java.io.IOException;
  15. import javax.microedition.midlet.MIDlet;
  16.  
  17. import com.sun.lwuit.Display;
  18. import com.sun.lwuit.Form;
  19. import com.sun.lwuit.Image;
  20. import com.sun.lwuit.plaf.UIManager;
  21. import com.sun.lwuit.util.Resources;
  22. import com.sun.lwuit.Graphics;
  23.  
  24. import com.mapdigit.gis.MapLayer;
  25. import com.mapdigit.gis.drawing.IGraphics;
  26. import com.mapdigit.gis.drawing.IImage;
  27. import com.mapdigit.gis.raster.IMapDrawingListener;
  28. import com.mapdigit.gis.raster.IReaderListener;
  29. import com.mapdigit.gis.raster.MapClient;
  30. import com.mapdigit.gis.raster.MapTileDownloadManager;
  31. import com.mapdigit.licence.LicenceManager;
  32.  
  33. import com.pstreets.gisengine.demo.lwuit.drawing.LWUITGraphicsFactory;
  34. import com.sun.lwuit.Painter;
  35. import com.sun.lwuit.geom.Rectangle;
  36.  
  37.  
  38. //[------------------------------ MAIN CLASS ----------------------------------]
  39. //--------------------------------- REVISIONS ----------------------------------
  40. // Date Name Tracking # Description
  41. // -------- ------------------- ------------- --------------------------
  42. // 11FEB2011 James Shen Initial Creation
  43. ////////////////////////////////////////////////////////////////////////////////
  44. /**
  45. * Base class for all Map Demos on LWUIT.
  46. * <hr><b>&copy; Copyright 2011 Guidebee, Inc. All Rights Reserved.</b>
  47. * @version 1.00, 11/02/11
  48. * @author Guidebee Pty Ltd.
  49. */
  50. public abstract class MapDemoLWUIT extends MIDlet implements IReaderListener,
  51. IMapDrawingListener {
  52.  
  53. protected MapClient map;
  54. protected MapTileDownloadManager mapTileDownloadManager;
  55. protected IImage mapImage;
  56. protected IGraphics mapGraphics;
  57. protected MapCanvas canvas;
  58.  
  59. public MapDemoLWUIT() {
  60. try {
  61. LicenceManager licenceManager = LicenceManager.getInstance();
  62. long keys[] = {0x34ba283b8daeb659L, -0x53c811f9da86e998L,
  63. -0x34ba25c3c581521eL, 0xf15df9fc7e45628L, 0x6a4ece44296c0287L,
  64. 0x4ab0cff532902b1cL,};
  65. licenceManager.addLicence("GuidebeeMap_JavaME", keys);
  66. } catch (Exception ex) {
  67. ex.printStackTrace();
  68. }
  69. }
  70.  
  71. protected void init() {
  72. Display.init(this);
  73. try {
  74. Resources r = Resources.open("/javaTheme.res");
  75. UIManager.getInstance().setThemeProps(
  76. r.getTheme(r.getThemeResourceNames()[0]));
  77. } catch (IOException ex) {
  78. ex.printStackTrace();
  79. }
  80. //set the graphics factory
  81. MapLayer.setAbstractGraphicsFactory(LWUITGraphicsFactory.getInstance());
  82. mapImage = MapLayer.getAbstractGraphicsFactory()
  83. .createImage(Display.getInstance().getDisplayWidth(),
  84. Display.getInstance().getDisplayHeight());
  85. mapGraphics = mapImage.getGraphics();
  86. //Create the Digital Map objects.
  87. mapTileDownloadManager = new MapTileDownloadManager(this);
  88. map = new MapClient(1024, 1024, mapTileDownloadManager);
  89. map.setScreenSize(Display.getInstance().getDisplayWidth(),
  90. Display.getInstance().getDisplayHeight());
  91. map.start();
  92. map.setMapDrawingListener(this);
  93. //Creat the main form.
  94. canvas = new MapCanvas("Hello world");
  95.  
  96. }
  97.  
  98. ////////////////////////////////////////////////////////////////////////////
  99. //--------------------------------- REVISIONS ------------------------------
  100. // Date Name Tracking # Description
  101. // --------- ------------------- ------------- ----------------------
  102. // 11FEB2011 James Shen Initial Creation
  103. ////////////////////////////////////////////////////////////////////////////
  104. /**
  105. * Used instead of using the Resources API to allow us to fetch locally
  106. * downloaded
  107. * resources
  108. *
  109. * @param name the name of the resource
  110. * @return a resources object
  111. */
  112. public Resources getResource(String name) throws IOException {
  113. return Resources.open("/" + name + ".res");
  114. }
  115.  
  116. public void pauseApp() {
  117. }
  118.  
  119. public void destroyApp(boolean unconditional) {
  120. map.stop();
  121. }
  122.  
  123. public void readProgress(int arg0, int arg1) {
  124. System.out.println(arg0 + "/" + arg1);
  125. }
  126.  
  127. public void done() {
  128. if (canvas != null) {
  129. canvas.repaint();
  130. }
  131. }
  132.  
  133. /**
  134. * Map canvas class ,a sub class of Form.
  135. */
  136. protected class MapCanvas extends Form {
  137.  
  138.  
  139. MapCanvas(String title) {
  140. super(title);
  141. }
  142.  
  143. public void paintBackground(Graphics g){
  144. map.paint(mapGraphics);
  145. g.drawImage((Image) mapImage.getNativeImage(), 0,
  146. 0);
  147. }
  148.  
  149. private void panMap(float x, float y) {
  150. float dx = x - oldX;
  151. float dy = y - oldY;
  152. if (!(dx == 0 && dy == 0)) {
  153. map.panDirection((int) dx, (int) dy);
  154. }
  155.  
  156. }
  157. boolean isPan = false;
  158. private float oldX = -1;
  159. private float oldY = -1;
  160.  
  161.  
  162. public void pointerDragged(int x, int y) {
  163. if (isPan) {
  164. panMap(x, y);
  165. oldX = x;
  166. oldY = y;
  167. }
  168. }
  169.  
  170. public void pointerPressed(int x, int y) {
  171. oldX = x;
  172. oldY = y;
  173. isPan = true;
  174. }
  175.  
  176. public void pointerReleased(int x, int y) {
  177. oldX = x;
  178. oldY = y;
  179. isPan = false;
  180. }
  181. }
  182. }

基类MapDemoLWUIT正确设置引路蜂License,并创建RasterMap地图。有了这些基类,第一个地图应用就非常简单,下面的例子显示中国南京地图。地图类型为Bing中国地图,缩放级别为13级。

  1. package com.pstreets.gisengine.demo.lwuit;
  2.  
  3. //--------------------------------- IMPORTS ------------------------------------
  4. import com.mapdigit.gis.geometry.GeoLatLng;
  5. import com.mapdigit.gis.raster.MapType;
  6. import com.pstreets.gisengine.demo.MapDemoLWUIT;
  7.  
  8. public class HelloWorldLWUIT extends MapDemoLWUIT {
  9.  
  10.     public void startApp() {
  11.         init();
  12.         canvas.show();
  13.         GeoLatLng center = new  GeoLatLng(32.0616667, 118.7777778);
  14.         map.setCenter(center, 13, MapType.MICROSOFTCHINA);
  15.     }
  16.    
  17. }