dd

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

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

使用BlackBerry JDE 6.0.0创建一个新的Workspace, RIMGISEngineTutorial. 首先添加一个新的Lib项目libGISEngine, 在其中添加引路蜂开发库。然后添加HelloWorldRIM Blackberry应用,将libGISEngine设为项目HelloWorldRIM的依赖库(Project Dependencies).

同样为简单起见,设计一个共用类MapDemoRIM作为后续例子的基类。

//--------------------------------- PACKAGE ------------------------------------
package com.pstreets.gisengine.demo;

//--------------------------------- IMPORTS ------------------------------------
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.RasterMap;
import com.mapdigit.licence.LicenceManager;
import com.pstreets.gisengine.demo.rim.drawing.RIMGraphicsFactory;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.TouchEvent;

//[------------------------------ MAIN CLASS ----------------------------------]
/**
 * Base class for all Map Demos.
 * <hr><b>&copy; Copyright 2011 Guidebee, Inc. All Rights Reserved.</b>
 * @version     1.00, 06/02/11
 * @author      Guidebee Pty Ltd.
 */
public abstract class MapDemoRIM extends UiApplication implements IReaderListener,
        IMapDrawingListener {

    protected RasterMap map;
    protected MapTileDownloadManager mapTileDownloadManager;
    protected IImage mapImage;
    protected IGraphics mapGraphics;
    protected MapCanvas canvas;

    public MapDemoRIM() {
        try {
            LicenceManager licenceManager = LicenceManager.getInstance();
            //expires on Tue Mar 15 20:54:42 CST 2011
           long keys[] = {0x34ba283b8daeb659L, -0x53c811f9da86e998L,
            -0x34ba25c3c581521eL, 0xf15df9fc7e45628L, 0x6a4ece44296c0287L,
            0x4ab0cff532902b1cL,};
            licenceManager.addLicence("GuidebeeMap_JavaME", keys);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public void init() {
        canvas = new MapCanvas();
        //set the graphics factory
        MapLayer.setAbstractGraphicsFactory(RIMGraphicsFactory.getInstance());
        mapImage = MapLayer.getAbstractGraphicsFactory()
                .createImage(Display.getWidth(),
                Display.getHeight());
        mapGraphics = mapImage.getGraphics();
        //Create the Digital Map objects.
        mapTileDownloadManager = new MapTileDownloadManager(this);
        try{
        map = new RasterMap(1024, 1024, mapTileDownloadManager);
    }catch(Exception e){}
        map.setScreenSize(Display.getWidth(),
                Display.getHeight());
        mapTileDownloadManager.start();
        map.setMapDrawingListener(this);

    }

    protected void createMenu(Menu menu, int instance){
    }
    public void closeApp() {
        mapTileDownloadManager.stop();
    }

    public void readProgress(int arg0, int arg1) {
        System.out.println(arg0 + "/" + arg1);
    }

    public void done() {
        if (canvas != null) {
            canvas.invalidate();
        }
    }

    protected class MapCanvas extends MainScreen {

        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);
            Bitmap bitmap=(Bitmap) mapImage.getNativeImage();
            g.drawBitmap(0,0,bitmap.getWidth(),bitmap.getHeight(),bitmap,0,0);
        }
       
        public void close(){
            closeApp();
        }
       
        protected boolean touchEvent(TouchEvent event){
            int action=event.getEvent();
          switch(action){
        case TouchEvent.DOWN:
                  oldX = event.getGlobalX(1);
            oldY = event.getGlobalY(1);
                    isPan=true;
                    break;
         case TouchEvent.UP:
                    oldX = event.getGlobalX(1);
            oldY = event.getGlobalY(1);
                    isPan=false;
                   break;
         case TouchEvent.MOVE :
                 if(isPan)
            {
            panMap(event.getGlobalX(1), event.getGlobalY(1));
            oldX = event.getGlobalX(1);
            oldY = event.getGlobalY(1);
            }
                    break;
         }
      return true;
        }
        protected void makeMenu(Menu menu, int instance){
            createMenu(menu,instance);
        }
    }
}

类MapDemoRIM派生于UiApplication,为Blackberry应用对象。MapCanvas派生于MainScreen,用于显示地图,并响应TouchEvent,可以平移地图。

有了这个基类,HelloWorldRIM就比较简单,下面的例子显示南京地图,缩放级别为13级,地图类型为Bing中国地图。

//--------------------------------- PACKAGE ------------------------------------
package com.pstreets.gisengine.demo.rim;

//--------------------------------- IMPORTS ------------------------------------
import com.mapdigit.gis.geometry.GeoLatLng;
import com.mapdigit.gis.raster.MapType;
import com.pstreets.gisengine.demo.MapDemoRIM;

//[------------------------------ MAIN CLASS ----------------------------------]
/**
 *  Hello world demo for Guidebee Map API on RIM platform.
 * <hr><b>&copy; Copyright 2011 Guidebee, Inc. All Rights Reserved.</b>
 * @version     1.00, 06/02/11
 * @author      Guidebee Pty Ltd.
 */
public class HelloWorldRIM extends MapDemoRIM {

    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args)
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        HelloWorldRIM theApp = new HelloWorldRIM();      
        theApp.enterEventDispatcher();
    }
   
    public HelloWorldRIM() {
        init();
        pushScreen(canvas);
        GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
        map.setCenter(center, 13, MapType.MICROSOFTCHINA);
    }
   
}

dd