在开发应用的过程中,常常需要在地图上需绘制一点自定义的兴趣点或是自定义的几何图形。引路蜂地图包中RasterMap最终提供的基实就是一张图片。
protected void paint(Graphics g) { map.paint(mapGraphics); g.drawImage((Image) mapImage.getNativeImage(), 0, 0, 0); //start drawing your own sharps or images. ... … }
所以一个简单的方法是绘制完地图后,你可以使用任何绘图方法在地图绘制任何自定义的图形或是图象。
这里需要注意的是坐标变换,RasterMap采用的是经纬度坐标,而屏幕显示采用的屏幕坐标,RasterMap提供了坐标转换的方法:fromScreenPixelToLatLng 坐屏幕坐标转换成地图经纬度坐标。fromLatLngToScreenPixel 从经纬度坐标转换成屏幕坐标。
下面给出的例子是采用派生MapLayer子类的方法,RasterMap是 MapLayerContainer的子类,可以用来管理多个地图层。这些图层从下到上相当于透明纸一层一层叠加形成最终显示的地图。
例子中显示几个自定义兴趣点,和一个三角形,并中地图中心显示十字标。
public class MapOverlayMIDP extends MapDemoMIDP { OverLayMapLayer mapLayer; public void startApp() { init(); GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778); map.setCenter(center, 9, MapType.GOOGLECHINA); Display.getDisplay(this).setCurrent(canvas); mapLayer = new OverLayMapLayer(canvas.getWidth(), canvas.getHeight()); map.addMapLayer(mapLayer); } class OverLayMapLayer extends MapLayer { GeoLatLng pt1 = new GeoLatLng(32.345281, 118.84261); GeoLatLng pt2 = new GeoLatLng(32.05899, 118.62789); GeoLatLng pt3 = new GeoLatLng(32.011811, 118.798656); public OverLayMapLayer(int width, int height) { super(width, height); } public void paint(IGraphics graphics, int offsetX, int offsetY) { drawCursor(graphics); drawTriangle(graphics); drawPoint(graphics, pt1); drawPoint(graphics, pt2); drawPoint(graphics, pt3); } public void drawTriangle(IGraphics g) { GeoPoint ptOnScreen1 = map.fromLatLngToScreenPixel(pt1); GeoPoint ptOnScreen2 = map.fromLatLngToScreenPixel(pt2); GeoPoint ptOnScreen3 = map.fromLatLngToScreenPixel(pt3); g.setColor(0x0000FF); g.drawLine((int) ptOnScreen1.x, (int) ptOnScreen1.y, (int) ptOnScreen2.x, (int) ptOnScreen2.y); g.drawLine((int) ptOnScreen2.x, (int) ptOnScreen2.y, (int) ptOnScreen3.x, (int) ptOnScreen3.y); g.drawLine((int) ptOnScreen1.x, (int) ptOnScreen1.y, (int) ptOnScreen3.x, (int) ptOnScreen3.y); } public void drawPoint(IGraphics g, GeoLatLng pt) { GeoPoint ptOnScreen = map.fromLatLngToScreenPixel(pt); int x = (int) ptOnScreen.x; int y = (int) ptOnScreen.y; g.setColor(0x00FF00); g.fillRect(x - 4, y - 4, 8, 8); } private void drawCursor(IGraphics g) { int x = getScreenWidth() / 2; int y = getScreenHeight() / 2; g.setColor(0x205020); g.drawRect(x - 4, y - 4, 8, 8); g.drawLine(x, y - 6, x, y - 2); g.drawLine(x, y + 6, x, y + 2); g.drawLine(x - 6, y, x - 2, y); g.drawLine(x + 6, y, x + 2, y); } } }