Android引路蜂地图开发示例:叠加自定义图层

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

在开发应用的过程中,常常需要在地图上需绘制一点自定义的兴趣点或是自定义的几何图形。引路蜂地图包中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的子类,可以用来管理多个地图层。这些图层从下到上相当于透明纸一层一层叠加形成最终显示的地图。
例子中显示几个自定义兴趣点,和一个三角形,并中地图中心显示十字标。

package com.pstreets.gisengine.demo;

import com.mapdigit.gis.MapLayer;
import com.mapdigit.gis.drawing.IGraphics;
import com.mapdigit.gis.geometry.GeoLatLng;
import com.mapdigit.gis.geometry.GeoPoint;
import com.mapdigit.gis.raster.MapType;
import com.pstreets.gisengine.GuidebeeMapView;
import com.pstreets.gisengine.R;
import com.pstreets.gisengine.SharedMapInstance;

import android.app.Activity;
import android.os.Bundle;

public class MapOverlay extends Activity {

  OverLayMapLayer mapLayer;
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 }

 public void onStart() {
  super.onStart();
  GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
  SharedMapInstance.map.setCenter(center, 9, MapType.MICROSOFTCHINA);
  mapLayer = new OverLayMapLayer(SharedMapInstance.map.getScreenWidth(),
    SharedMapInstance.map.getScreenHeight());
  SharedMapInstance.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 = SharedMapInstance.map.fromLatLngToScreenPixel(pt1);
            GeoPoint ptOnScreen2 = SharedMapInstance.map.fromLatLngToScreenPixel(pt2);
            GeoPoint ptOnScreen3 = SharedMapInstance.map.fromLatLngToScreenPixel(pt3);
            g.setColor(0xFF0000FF);
           
            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 = SharedMapInstance.map.fromLatLngToScreenPixel(pt);
            int x = (int) ptOnScreen.x;
            int y = (int) ptOnScreen.y;
            g.setColor(0xFF00FF00);
            g.fillRect(x - 4, y - 4, 8, 8);

        }

        private void drawCursor(IGraphics g) {
           
            int mapWidth=getScreenWidth();
      int mapHeight=getScreenHeight();
      int x = mapWidth;
      int y = mapHeight;
            g.setColor(0xFF205020);
            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);
        }
    }

}