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

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

在开发应用的过程中,常常需要在地图上需绘制一点自定义的兴趣点或是自定义的几何图形。引路蜂地图包中RasterMap最终提供的基实就是一张图片。

  1. protected void paint(Graphics g){
  2. map.paint(mapGraphics);
  3. g.drawImage((Image) mapImage.getNativeImage(), 0, 0, 0);        
  4. //start drawing your own sharps or images.
  5. ...
  6. }

所以一个简单的方法是绘制完地图后,你可以使用任何绘图方法在地图绘制任何自定义的图形或是图象。
这里需要注意的是坐标变换,RasterMap采用的是经纬度坐标,而屏幕显示采用的屏幕坐标,RasterMap提供了坐标转换的方法:fromScreenPixelToLatLng 坐屏幕坐标转换成地图经纬度坐标。fromLatLngToScreenPixel 从经纬度坐标转换成屏幕坐标。
下面给出的例子是采用派生MapLayer子类的方法,RasterMap是 MapLayerContainer的子类,可以用来管理多个地图层。这些图层从下到上相当于透明纸一层一层叠加形成最终显示的地图。
例子中显示几个自定义兴趣点,和一个三角形,并中地图中心显示十字标。

  1. package com.pstreets.gisengine.demo;
  2.  
  3. import com.mapdigit.gis.MapLayer;
  4. import com.mapdigit.gis.drawing.IGraphics;
  5. import com.mapdigit.gis.geometry.GeoLatLng;
  6. import com.mapdigit.gis.geometry.GeoPoint;
  7. import com.mapdigit.gis.raster.MapType;
  8. import com.pstreets.gisengine.GuidebeeMapView;
  9. import com.pstreets.gisengine.R;
  10. import com.pstreets.gisengine.SharedMapInstance;
  11.  
  12. import android.app.Activity;
  13. import android.os.Bundle;
  14.  
  15. public class MapOverlay extends Activity {
  16.  
  17.   OverLayMapLayer mapLayer;
  18.  public void onCreate(Bundle savedInstanceState) {
  19.   super.onCreate(savedInstanceState);
  20.   setContentView(R.layout.main);
  21.  }
  22.  
  23.  public void onStart() {
  24.   super.onStart();
  25.   GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
  26.   SharedMapInstance.map.setCenter(center, 9, MapType.MICROSOFTCHINA);
  27.   mapLayer = new OverLayMapLayer(SharedMapInstance.map.getScreenWidth(),
  28.     SharedMapInstance.map.getScreenHeight());
  29.   SharedMapInstance.map.addMapLayer(mapLayer);
  30.  
  31.  }
  32.  
  33.  class OverLayMapLayer extends MapLayer {
  34.  
  35.         GeoLatLng pt1 = new GeoLatLng(32.345281, 118.84261);
  36.         GeoLatLng pt2 = new GeoLatLng(32.05899, 118.62789);
  37.         GeoLatLng pt3 = new GeoLatLng(32.011811, 118.798656);
  38.  
  39.         public OverLayMapLayer(int width, int height) {
  40.             super(width, height);
  41.         }
  42.  
  43.         public void paint(IGraphics graphics, int offsetX, int offsetY) {
  44.             drawCursor(graphics);
  45.             drawTriangle(graphics);
  46.             drawPoint(graphics, pt1);
  47.             drawPoint(graphics, pt2);
  48.             drawPoint(graphics, pt3);
  49.  
  50.         }
  51.  
  52.         public void drawTriangle(IGraphics g) {
  53.             GeoPoint ptOnScreen1 = SharedMapInstance.map.fromLatLngToScreenPixel(pt1);
  54.             GeoPoint ptOnScreen2 = SharedMapInstance.map.fromLatLngToScreenPixel(pt2);
  55.             GeoPoint ptOnScreen3 = SharedMapInstance.map.fromLatLngToScreenPixel(pt3);
  56.             g.setColor(0xFF0000FF);
  57.            
  58.             g.drawLine((int) ptOnScreen1.x, (int) ptOnScreen1.y,
  59.                     (int) ptOnScreen2.x, (int) ptOnScreen2.y);
  60.             g.drawLine((int) ptOnScreen2.x, (int) ptOnScreen2.y,
  61.                     (int) ptOnScreen3.x, (int) ptOnScreen3.y);
  62.             g.drawLine((int) ptOnScreen1.x, (int) ptOnScreen1.y,
  63.                     (int) ptOnScreen3.x, (int) ptOnScreen3.y);
  64.         }
  65.  
  66.         public void drawPoint(IGraphics g, GeoLatLng pt) {
  67.             GeoPoint ptOnScreen = SharedMapInstance.map.fromLatLngToScreenPixel(pt);
  68.             int x = (int) ptOnScreen.x;
  69.             int y = (int) ptOnScreen.y;
  70.             g.setColor(0xFF00FF00);
  71.             g.fillRect(x - 4, y - 4, 8, 8);
  72.  
  73.         }
  74.  
  75.         private void drawCursor(IGraphics g) {
  76.            
  77.             int mapWidth=getScreenWidth();
  78.       int mapHeight=getScreenHeight();
  79.       int x = mapWidth;
  80.       int y = mapHeight;
  81.             g.setColor(0xFF205020);
  82.             g.drawRect(x - 4, y - 4, 8, 8);
  83.             g.drawLine(x, y - 6, x, y - 2);
  84.             g.drawLine(x, y + 6, x, y + 2);
  85.             g.drawLine(x - 6, y, x - 2, y);
  86.             g.drawLine(x + 6, y, x + 2, y);
  87.         }
  88.     }
  89.  
  90. }