3

有人可以帮我弄清楚如何在 上绘制路线RichMapField,我可以在MapField.

我想使用RichMapField,因为我可以使用MapDataModel动态添加多个标记。

更新代码:

这是我尝试编写代码以在 a 上显示从 A 到 B 的路线RichMapField,我得到的只是地图上的一个点。有人可以帮我解决这个问题:

class MapPathScreen extends MainScreen {

    MapControl map;
    Road mRoad = new Road();
    RichMapField mapField = MapFactory.getInstance().generateRichMapField();

    public MapPathScreen() {
        double fromLat = 47.67, fromLon = 9.38, toLat =47.12, toLon = 9.47;
        /* double fromLat = 49.85, fromLon = 24.016667;
        double toLat = 50.45, toLon = 30.523333;
        */
        String url = RoadProvider.getUrl(fromLat, fromLon, toLat, toLon);
        InputStream is = getConnection(url);
        mRoad = RoadProvider.getRoute(is);
        map = new MapControl(mapField);
        add(new LabelField(mRoad.mName));
        add(new LabelField(mRoad.mDescription));
        add(map);
    }

    protected void onUiEngineAttached(boolean attached) {
        super.onUiEngineAttached(attached);
        if (attached) {
            map.drawPath(mRoad);
        }
    }

    private InputStream getConnection(String url) {
        HttpConnection urlConnection = null;
        InputStream is = null;
        try {
            urlConnection = (HttpConnection) Connector.open(url);
            urlConnection.setRequestMethod("GET");
            is = urlConnection.openInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return is;
    }

    protected boolean keyDown(int keycode, int time)
    {
        MapAction action=mapField.getAction();
        StringBuffer sb = new StringBuffer();
        // Retrieve the characters mapped to the keycode for the current keyboard layout
        Keypad.getKeyChars(keycode, sb);
        // Zoom in
        if(sb.toString().indexOf('i') != -1)
        {
            action.zoomIn();
            return true;
        }
        // Zoom out
        else if(sb.toString().indexOf('o') != -1)
        {
            action.zoomOut();
            return true;
        }
        return super.keyDown(keycode, time);
    }
}

class MapControl extends net.rim.device.api.lbs.maps.ui.MapField {

    Bitmap bmp = null;
    MapAction action;
    MapField map = new MapField();
    RichMapField mapRich;
    Road road;

    public MapControl(RichMapField mapRich)
    {
        this.mapRich = mapRich;
    }

    public void drawPath(Road road) {
        if (road.mRoute.length > 0) {
            Coordinates[] mPoints = new Coordinates[] {};
            for (int i = 0; i < road.mRoute.length; i++) {
                Arrays.add(mPoints, new Coordinates(road.mRoute[i][1],
                road.mRoute[i][0], 0));
            }
            double moveToLat = mPoints[0].getLatitude()
                + (mPoints[mPoints.length - 1].getLatitude() - mPoints[0].getLatitude()) / 2;
            double moveToLong = mPoints[0].getLongitude()
                + (mPoints[mPoints.length - 1].getLongitude() - mPoints[0].getLongitude()) / 2;
            Coordinates moveTo = new Coordinates(moveToLat, moveToLong, 0);
            action = this.getAction();
            action.setZoom(15);
            action.setCentreAndZoom(new MapPoint(moveToLat,moveToLong), 15);
            bmp = new Bitmap(500, 500);
            bmp.createAlpha(Bitmap.ALPHA_BITDEPTH_8BPP);
            Graphics g = Graphics.create(bmp);
            int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
            XYPoint point = new XYPoint();
            Coordinates c  = new Coordinates(mPoints[0].getLatitude(),mPoints[0].getLongitude(),0);
            map.convertWorldToField(c, point);
            x1=point.x;
            y1 = point.y;
            g.fillEllipse(x1, y1, x1, y1 + 1, x1 + 1, y1, 0, 360);
            for (int i = 1; i < mPoints.length; i++) {
                XYPoint point1 = new XYPoint();
                Coordinates c1  = new Coordinates(mPoints[i].getLatitude(),mPoints[i].getLongitude(),0);
                map.convertWorldToField(c1, point1);
                x2 = point1.x;
                y2 = point1.y;
                g.setColor(Color.GREEN);
                //g.fillEllipse(x1, y1, x1, y1 + 1, x1 + 1, y1, 0, 360);
                g.drawLine(x1, y1, x2, y2);
                x1 = x2;
                y1 = y2;
            }
        }
    }

    protected void paint(Graphics g) {
        super.paint(g);
        if (bmp != null) {
            g.setGlobalAlpha(100);
            g.drawBitmap(0, 0, bmp.getWidth(), bmp.getHeight(), bmp, 0, 0);
        }
    }
4

0 回答 0