Android引路蜂地圖開發示例:疊加自定義圖層

在開發應用的過程中,常常需要在地圖上需繪制一點自定義的興趣點或是自定義的幾何圖形。引路蜂地圖包中RasterMap最終提供的基實就是一張圖片。

[java] 
protected void paint(Graphics g){ 
    map.paint(mapGraphics); 
    g.drawImage((Image) mapImage.getNativeImage(), 0, 0, 0);         
    //start drawing your own sharps or images.  
    … … 
    } 
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的子類,可以用來管理多個地圖層。這些圖層從下到上相當於透明紙一層一層疊加形成最終顯示的地圖。
例子中顯示幾個自定義興趣點,和一個三角形,並中地圖中心顯示十字標。

[java] 
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); 
        } 
    } 
  

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);
        }
    }
 
}


作者:mapdigit

 

發佈留言