下面的代码显示中国南京地图
- //------------------------------------------------------------------------------
- // COPYRIGHT 2011 GUIDEBEE
- // ALL RIGHTS RESERVED.
- // GUIDEBEE CONFIDENTIAL PROPRIETARY
- ///////////////////////////////////// REVISIONS ////////////////////////////////
- // Date Name Tracking # Description
- // --------- ------------------- ---------- --------------------------
- // 28JAN2011 James Shen Initial Creation
- ////////////////////////////////////////////////////////////////////////////////
- //--------------------------------- PACKAGE ------------------------------------
- package com.pstreets.gisengine.demo.midp;
- //--------------------------------- IMPORTS ------------------------------------
- import javax.microedition.midlet.MIDlet;
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- import javax.microedition.lcdui.Display;
- import com.mapdigit.gis.MapLayer;
- import com.mapdigit.gis.drawing.IGraphics;
- import com.mapdigit.gis.drawing.IImage;
- import com.mapdigit.gis.geometry.GeoLatLng;
- import com.mapdigit.gis.raster.IMapDrawingListener;
- import com.mapdigit.gis.raster.IReaderListener;
- import com.mapdigit.gis.raster.MapTileDownloadManager;
- import com.mapdigit.gis.raster.MapType;
- import com.mapdigit.gis.raster.RasterMap;
- import com.mapdigit.licence.LicenceManager;
- import com.pstreets.gisengine.demo.midp.drawing.MIDPGraphicsFactory;
- //[------------------------------ MAIN CLASS ----------------------------------]
- /**
- * Hello China demo.
- * <hr><b>© Copyright 2011 Guidebee, Inc. All Rights Reserved.</b>
- * @version 1.00, 28/01/11
- * @author Guidebee Pty Ltd.
- */
- public class HelloChina extends MIDlet implements IReaderListener,
- IMapDrawingListener {
- protected RasterMap map;
- protected MapTileDownloadManager mapTileDownloadManager;
- protected IImage mapImage;
- protected IGraphics mapGraphics;
- protected MapCanvas canvas;
- public HelloChina() {
- try {
- LicenceManager licenceManager = LicenceManager.getInstance();
- long keys[] = {0x34ba283b8daeb659L, -0x53c811f9da86e998L,
- -0x34ba25c3c581521eL, 0xf15df9fc7e45628L, 0x6a4ece44296c0287L,
- 0x4ab0cff532902b1cL,};
- licenceManager.addLicence("GuidebeeMap_JavaME", keys);
- } catch (Exception ex) {
- }
- }
- public void startApp() {
- canvas = new MapCanvas();
- //set the graphics factory
- MapLayer.setAbstractGraphicsFactory(MIDPGraphicsFactory.getInstance());
- mapImage = MapLayer.getAbstractGraphicsFactory()
- .createImage(canvas.getWidth(),
- canvas.getHeight());
- mapGraphics = mapImage.getGraphics();
- //Create the Digital Map objects.
- mapTileDownloadManager = new MapTileDownloadManager(this);
- try {
- map = new RasterMap(1024, 1024, mapTileDownloadManager);
- } catch (Exception ex) {
- }
- map.setScreenSize(canvas.getWidth(),
- canvas.getHeight());
- mapTileDownloadManager.start();
- map.setMapDrawingListener(this);
- GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
- map.setCenter(center, 13, MapType.GOOGLECHINA);
- Display.getDisplay(this).setCurrent(canvas);
- }
- public void pauseApp() {
- }
- public void destroyApp(boolean unconditional) {
- mapTileDownloadManager.stop();
- }
- public void readProgress(int downloaded, int total) {
- System.out.println(downloaded + "/" + total);
- }
- public void done() {
- if (canvas != null) {
- canvas.repaint();
- }
- }
- /**
- * Map canvas class, a subclass of Canvas.
- */
- protected class MapCanvas extends Canvas {
- private void panMap(float x, float y) {
- float dx = x - oldX;
- float dy = y - oldY;
- if (!(dx == 0 && dy == 0)) {
- map.panDirection((int) dx, (int) dy);
- }
- }
- boolean isPan = false;
- private float oldX = -1;
- private float oldY = -1;
- protected void paint(Graphics g) {
- map.paint(mapGraphics);
- g.drawImage((Image) mapImage.getNativeImage(), 0, 0, 0);
- }
- public void pointerDragged(int x, int y) {
- if (isPan) {
- panMap(x, y);
- oldX = x;
- oldY = y;
- }
- }
- public void pointerPressed(int x, int y) {
- oldX = x;
- oldY = y;
- isPan = true;
- }
- public void pointerReleased(int x, int y) {
- oldX = x;
- oldY = y;
- isPan = false;
- }
- }
- }
在后面的示例中,一些共同的代码不再重复,将设计一个基类MapDemoMIDP,创建地图,设置使用许可等代码都放在基类中.
- //------------------------------------------------------------------------------
- // COPYRIGHT 2010 GUIDEBEE
- // ALL RIGHTS RESERVED.
- // GUIDEBEE CONFIDENTIAL PROPRIETARY
- ///////////////////////////////////// REVISIONS ////////////////////////////////
- // Date Name Tracking # Description
- // --------- ------------------- ---------- --------------------------
- // 28JAN2011 James Shen Initial Creation
- ////////////////////////////////////////////////////////////////////////////////
- //--------------------------------- PACKAGE ------------------------------------
- package com.pstreets.gisengine.demo;
- //--------------------------------- IMPORTS ------------------------------------
- import javax.microedition.midlet.MIDlet;
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- import com.mapdigit.gis.MapLayer;
- import com.mapdigit.gis.drawing.IGraphics;
- import com.mapdigit.gis.drawing.IImage;
- import com.mapdigit.gis.raster.IMapDrawingListener;
- import com.mapdigit.gis.raster.IReaderListener;
- import com.mapdigit.gis.raster.MapTileDownloadManager;
- import com.mapdigit.gis.raster.MapType;
- import com.mapdigit.gis.raster.RasterMap;
- import com.mapdigit.licence.LicenceManager;
- import com.pstreets.gisengine.demo.midp.drawing.MIDPGraphicsFactory;
- //[------------------------------ MAIN CLASS ----------------------------------]
- //--------------------------------- REVISIONS ----------------------------------
- // Date Name Tracking # Description
- // -------- ------------------- ------------- --------------------------
- // 28JAN2011 James Shen Initial Creation
- ////////////////////////////////////////////////////////////////////////////////
- /**
- * Base class for all Map Demos.
- * <hr><b>© Copyright 2010 Guidebee, Inc. All Rights Reserved.</b>
- * @version 1.00, 18/09/10
- * @author Guidebee Pty Ltd.
- */
- public abstract class MapDemoMIDP extends MIDlet implements IReaderListener,
- IMapDrawingListener {
- protected RasterMap map;
- protected MapTileDownloadManager mapTileDownloadManager;
- protected IImage mapImage;
- protected IGraphics mapGraphics;
- protected MapCanvas canvas;
- public MapDemoMIDP() {
- try {
- LicenceManager licenceManager = LicenceManager.getInstance();
- long keys[] = {0x34ba283b8daeb659L, -0x53c811f9da86e998L,
- -0x34ba25c3c581521eL, 0xf15df9fc7e45628L, 0x6a4ece44296c0287L,
- 0x4ab0cff532902b1cL,};
- licenceManager.addLicence("GuidebeeMap_JavaME", keys);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- /**
- * this only used for the vector map demo.
- */
- public void initCanvas() {
- canvas = new MapCanvas();
- }
- public void init() {
- canvas = new MapCanvas();
- //set the graphics factory
- MapLayer.setAbstractGraphicsFactory(MIDPGraphicsFactory.getInstance());
- mapImage = MapLayer.getAbstractGraphicsFactory()
- .createImage(canvas.getWidth(),
- canvas.getHeight());
- mapGraphics = mapImage.getGraphics();
- //Create the Digital Map objects.
- mapTileDownloadManager = new MapTileDownloadManager(this);
- MapType.updateMapTileUrl();
- map = new RasterMap(1024, 1024, mapTileDownloadManager);
- map.setScreenSize(canvas.getWidth(),
- canvas.getHeight());
- mapTileDownloadManager.start();
- map.setMapDrawingListener(this);
- }
- public void pauseApp() {
- }
- public void destroyApp(boolean unconditional) {
- mapTileDownloadManager.stop();
- }
- public void readProgress(int arg0, int arg1) {
- System.out.println(arg0 + "/" + arg1);
- }
- public void done() {
- if (canvas != null) {
- canvas.repaint();
- }
- }
- /**
- * Map canvas class, a subclass of Canvas.
- */
- protected class MapCanvas extends Canvas {
- private void panMap(float x, float y) {
- float dx = x - oldX;
- float dy = y - oldY;
- if (!(dx == 0 && dy == 0)) {
- map.panDirection((int) dx, (int) dy);
- }
- }
- boolean isPan = false;
- private float oldX = -1;
- private float oldY = -1;
- protected void paint(Graphics g) {
- map.paint(mapGraphics);
- g.drawImage((Image) mapImage.getNativeImage(), 0, 0, 0);
- }
- public void pointerDragged(int x, int y) {
- if (isPan) {
- panMap(x, y);
- oldX = x;
- oldY = y;
- }
- }
- public void pointerPressed(int x, int y) {
- oldX = x;
- oldY = y;
- isPan = true;
- }
- public void pointerReleased(int x, int y) {
- oldX = x;
- oldY = y;
- isPan = false;
- }
- }
- }