super

          android mapView中畫軌跡的overlay


          使用方法:
          LineItemizedOverlay overlay = new LineItemizedOverlay();

          overlay.addOverlay(/*起點的OverlayItem*/);
          overlay.addOverlay(/*終點的OverlayItem*/);
          overlay.addLinePoint(/*要畫的軌跡的GeoPoint的List*/);

          mapView.getOverlays().add(overlay);

          /**
           *
           */
          package com.xtyon.tuola.truck.map;

          import java.util.ArrayList;

          import android.graphics.Canvas;
          import android.graphics.Color;
          import android.graphics.Paint;
          import android.graphics.Point;
          import android.graphics.drawable.Drawable;

          import com.google.android.maps.GeoPoint;
          import com.google.android.maps.ItemizedOverlay;
          import com.google.android.maps.MapView;
          import com.google.android.maps.OverlayItem;
          import com.google.android.maps.Projection;

          /**
           * 地圖上的線型圖層:包括一個起點,一個終點,以及之間的曲線
           * @author superwang
           */
          public class LineItemizedOverlay extends ItemizedOverlay<OverlayItem> {
           private static final int LAYER_FLAGS = Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG
             | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG;
           /**
            * 用于保存起點/終點數據
            */
           private final ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

           /**
            * 用于保存構成曲線的點的數據
            */
           private final ArrayList<GeoPoint> linePoints = new ArrayList<GeoPoint>();

           /**
            * @param defaultMarker
            */
           public LineItemizedOverlay() {
            super(null);

            // TODO Auto-generated constructor stub
           }

           /* (non-Javadoc)
            * @see com.google.android.maps.ItemizedOverlay#createItem(int)
            */
           @Override
           protected OverlayItem createItem(int i) {
            return mOverlays.get(i);

           }

           /* (non-Javadoc)
            * @see com.google.android.maps.ItemizedOverlay#size()
            */
           @Override
           public int size() {
            // TODO Auto-generated method stub
            return mOverlays.size();
           }

           /**
            * 調價起點/終點
            * description:
            * @param overlay
            */
           public void addOverlay(OverlayItem overlay) {
            mOverlays.add(overlay);
            populate();
           }

           /**
            * 添加曲線中的點
            * description:
            * @param point
            */
           public void addLinePoint(GeoPoint point) {
            linePoints.add(point);
           }

           public ArrayList<GeoPoint> getLinePoints() {
            return linePoints;
           }

           /**
            * 畫起點/終點/軌跡
            */
           @Override
           public void draw(Canvas canvas, MapView mapView, boolean shadow) {
            if (!shadow) {
             //System.out.println("!!!!!!!!!!!!!!");

             canvas.save(LAYER_FLAGS);
             //canvas.save();

             Projection projection = mapView.getProjection();
             int size = mOverlays.size();
             Point point = new Point();
             Paint paint = new Paint();
             paint.setAntiAlias(true);
             OverlayItem overLayItem;

             //畫起點/終點
             for (int i = 0; i < size; i++) {

              overLayItem = mOverlays.get(i);

              Drawable marker = overLayItem.getMarker(0);
              //marker.getBounds()
              /* 象素點取得轉換 */
              projection.toPixels(overLayItem.getPoint(), point);

              if (marker != null) {
               boundCenterBottom(marker);
              }

              /* 圓圈 */
              //Paint paintCircle = new Paint();
              //paintCircle.setColor(Color.RED);
              paint.setColor(Color.RED);
              canvas.drawCircle(point.x, point.y, 5, paint);

              /* 文字設置 */
              /* 標題 */
              String title = overLayItem.getTitle();
              /* 簡介 */
              //    String snippet = overLayItem.getSnippet();
              //
              //    StringBuffer txt = new StringBuffer();
              //    if (title != null && !"".equals(title))
              //     txt.append(title);
              //
              //    if (snippet != null && !"".equals(snippet)) {
              //     if (txt.length() > 0) {
              //      txt.append(":");
              //     }
              //     txt.append(snippet);
              //    }    
              //Paint paintText = new Paint();

              if (title != null && title.length() > 0) {
               paint.setColor(Color.BLACK);
               paint.setTextSize(15);
               canvas.drawText(title, point.x, point.y, paint);
              }

             }

             //畫線

             boolean prevInBound = false;//前一個點是否在可視區域
             Point prev = null;
             int mapWidth = mapView.getWidth();
             int mapHeight = mapView.getHeight();
             //Paint paintLine = new Paint();
             paint.setColor(Color.RED);
             //paint.setPathEffect(new CornerPathEffect(10));
             paint.setStrokeWidth(2);
             int count = linePoints.size();

             //Path path = new Path();
             //path.setFillType(Path.FillType.INVERSE_WINDING);
             for (int i = 0; i < count; i++) {
              GeoPoint geoPoint = linePoints.get(i);
              //projection.toPixels(geoPoint, point); //這一行似乎有問題
              point = projection.toPixels(geoPoint, null);
              if (prev != null) {
               if (point.x >= 0 && point.x <= mapWidth && point.y >= 0 && point.y <= mapHeight) {
                if ((Math.abs(prev.x - point.x) > 2 || Math.abs(prev.y - point.y) > 2)) {
                 //這里判斷點是否重合,重合的不畫線,可能會導致畫線不在路上
                 canvas.drawLine(prev.x, prev.y, point.x, point.y, paint);
                 //path.lineTo(point.x, point.y);

                 prev = point;
                 prevInBound = true;

                }
               } else {
                //在可視區與之外
                if (prevInBound) {//前一個點在可視區域內,也需要劃線
                 //path.lineTo(point.x, point.y);
                 canvas.drawLine(prev.x, prev.y, point.x, point.y, paint);
                }
                prev = point;
                prevInBound = false;
               }
              } else {
               //path.moveTo(point.x, point.y);
               prev = point;

              }
             }
             //canvas.drawPath(path, paint);
             canvas.restore();
             //DebugUtils.showMemory();
            }
            super.draw(canvas, mapView, shadow);
           }

          }

          posted on 2010-08-12 14:21 王衛華 閱讀(1683) 評論(0)  編輯  收藏 所屬分類: android


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 枣强县| 隆德县| 达日县| 轮台县| 谷城县| 舞阳县| 乌鲁木齐市| 米泉市| 石河子市| 吴堡县| 武定县| 新津县| 蚌埠市| 北辰区| 西峡县| 乌审旗| 和平县| 交口县| 大英县| 历史| 临安市| 县级市| 崇文区| 南丰县| 长白| 油尖旺区| 开化县| 姜堰市| 太康县| 景德镇市| 化州市| 达日县| 镇安县| 拉萨市| 威远县| 黄石市| 敖汉旗| 阿拉尔市| 五华县| 高唐县| 九江市|