2011年6月24日金曜日

GoogleMap Overlay 

オーバーレイを地図の上に重ね合わせることで、地図上の情報を画像やマークなどの形で表示することができます。たとえば、飲食店の位置にマークを追加することで、ユーザーは店までの経路を把握することができます。

オーバーレイ機能の追加方法を説明します。

方法一:
1.Overlayを定義するクラス

public class MyPositionOverlay extends Overlay {
    private GeoPoint geoPoint;
    private Context context;
    private int drawable;
    public MyPositionOverlay(GeoPoint geoPoint, Context context, int drawable) {
        super();
        this.geoPoint = geoPoint;
        this.context = context;
        this.drawable = drawable;
    }
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        Projection projection = mapView.getProjection();
        Point point = new Point();
        projection.toPixels(geoPoint, point);
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
                drawable);
        canvas.drawBitmap(bitmap, point.x  , point.y
                - bitmap.getHeight() , null);
        super.draw(canvas, mapView, shadow);
    }
}

2.ViewMapクラス

public class ViewMap extends MapActivity{
  private MapView mapView ;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.view_map);
  mapView =(MapView)findViewById(R.id.map);
  mapView.setBuiltInZoomControls(true);
  //オーバーレイの表示
  MyPositionOverlay overlay =new MyPositionOverlay(new GeoPoint(idoInt,keidoInt),
    this, R.drawable.here);
  List<Overlay> overlays = mapView.getOverlays();
  overlays.add(overlay);
 }

 @Override
 protected boolean isRouteDisplayed() {
  // TODO 自動生成されたメソッド・スタブ
  return false;
 }

}

方法二:
1.OverlaySampleクラス
public class OverlaySample extends ItemizedOverlay<OverlayItem> {
 ArrayList<OverlayItem> overlayItems = new ArrayList<OverlayItem>();
 public OverlaySample(Drawable arg0) {
  super(arg0);
 }
 public OverlaySample(Drawable defaultMarker , Context context ) {
  super(boundCenterBottom(defaultMarker));
 }
 @Override
 protected OverlayItem createItem(int i) {
  return overlayItems.get(i);
 }
 @Override
 public int size() {
  return overlayItems.size();
 }
 @Override
 protected boolean onTap(int index) {
  System.out.println(overlayItems.get(index));
  return super.onTap(index);
 }
 public void addOverlay(OverlayItem overlayitem){
  overlayItems.add(overlayitem);
  populate();
 }
}
2.mapグラス
public class map extends MapActivity {
      @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MapView mapView =(MapView)findViewById(R.id.map);
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = getResources().getDrawable(R.drawable.icon);
        OverlaySample overlaySample = new OverlaySample(drawable , this);
        GeoPoint point =new GeoPoint( 999999,111111);
        OverlayItem overlayItem = new OverlayItem(point, "title", "content");
        overlaySample.addOverlay(overlayItem);
        mapOverlays.add(overlaySample);

0 件のコメント: