下面的代码显示中国南京地图
//------------------------------------------------------------------------------
// 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;
}
}
}
- 首先是设置使用许可(Licence),设置使用许可由二部分组成,一个是将licence文件(guidebee.lic)放在合适的目录下,然后在程序中输入正确的注册码。Ln 51-55
- 前文说过,引路蜂地图开发包不直接调用图形相关方法,而是定义了一些接口函数,在使用时,需实现这些平台相关的方法。例如在 Java ME平台,MIDPFont, MIDPGraphics,MIDPGraphicsFactory,MIDPImage 实现了这些接口方法,在调用任务地图相关方法,需要告诉地图如何调用这些方法。MapLayer.setAbstractGraphicsFactory(MIDPGraphicsFactory.getInstance());则是用来设置平台相关GraphicsFactory实现。Ln 64-67
- 创建MapTileDownloaderManager 实例,MapTileDownloaderManager是用来管理工作线程以下载地图图片或是从本地地图包中读取地图图片。之后创建RasterMap 地图实例,RasterMap使用MapTileDownloaderManager来获取地图图片。Ln 70-Ln74
- MapTileDownloaderManager.Start 启动工作线程。注意在程序结束时,需调用Stop 方法来停止这些工作线程,如(6) Ln 77
- 设置地图中心点,这里设置是南京的经纬度坐标。地图类型选择是Google中国地图,地图包中定义了多达20多种不同的地图可供选择。地图缩放级别为13级。1级最小,17级最大。 Ln 79 -80
- 程序结束时,需调用Stop 方法来停止工作线程。 Ln 88
- panDirection(dx,dy)用来平移地图,将在后面介绍。 Ln 110
- 在屏幕上显示地图。在这个应用中 MapCanvas 最终用来在屏幕上显示地图,每个程序都有一个UI界面,地图最终总是要在屏幕上显示的。这两行代码是将地图复制到屏幕上。 Ln 119 -120
- 地图下载采取异步工作方式,也就是地图图片的读取是通过工作线程在后台工作,应用可以监视读取图片的进度readProgress,每张地图图片下载完毕时,会使用消息Done通知应用,在Done消息处理中一般用来更新屏幕显示。Ln 91 -99
在后面的示例中,一些共同的代码不再重复,将设计一个基类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;
}
}
}