Android教程:在百度地图上画出轨迹(2)

// 创建点击mark时的弹出泡泡
   mPopView = super.getLayoutInflater()
     .inflate(R.layout.popview, null);
   mMapView.addView(mPopView, new MapView.LayoutParams(
     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, null,
     MapView.LayoutParams.TOP_LEFT));
   mPopView.setVisibility(View.GONE);
  }

}

@Override
 protected void onPause() {
  BMapApp app = (BMapApp) this.getApplication();
  app.mBMapMan.getLocationManager().removeUpdates(mLocationListener);
  mLocationOverlay.disableMyLocation();
  mLocationOverlay.disableCompass(); // 关闭指南针
  app.mBMapMan.stop();
  super.onPause();
 }

@Override
 protected void onResume() {
  BMapApp app = (BMapApp) this.getApplication();
  // 注册定位事件,定位后将地图移动到定位点
  app.mBMapMan.getLocationManager().requestLocationUpdates(
    mLocationListener);
  mLocationOverlay.enableMyLocation();
  mLocationOverlay.enableCompass(); // 打开指南针
  app.mBMapMan.start();
  super.onResume();
 }

@Override
 protected boolean isRouteDisplayed() {
  // TODO Auto-generated method stub
  return false;
 }

}

class OverItemT extends ItemizedOverlay<OverlayItem> {

private List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();
 private Drawable marker;
 private Context mContext;
 private ArrayList<Trace> traces = new ArrayList<Trace>();

public OverItemT(Drawable marker, Context context, ArrayList<Trace> traces) {
  super(boundCenterBottom(marker));

this.marker = marker;
  this.mContext = context;
  this.traces = traces;
  // 用给定的经纬度构造GeoPoint,单位是微度 (度 * 1E6)
  // 构造OverlayItem的三个参数依次为:item的位置,标题文本,文字片段
  for (int i = 0; i < traces.size(); i++) {
   int lat = new Integer(traces.get(i).getLatitude());
   int lon = new Integer(traces.get(i).getLongitude());
   String resName = traces.get(i).getResname();
   GeoPoint p = new GeoPoint(lat, lon);
   mGeoList.add(new OverlayItem(p, resName, i + resName));
   populate(); // createItem(int)方法构造item。一旦有了数据,在调用其它方法前,首先调用这个方法
  }

}

@Override
 public void draw(Canvas canvas, MapView mapView, boolean shadow) {

// Projection接口用于屏幕像素坐标和经纬度坐标之间的变换
  Projection projection = mapView.getProjection();
  for (int index = size() - 1; index >= 0; index--) { // 遍历mGeoList
   OverlayItem overLayItem = getItem(index); // 得到给定索引的item

String title = overLayItem.getTitle();
   // 把经纬度变换到相对于MapView左上角的屏幕像素坐标
   Point point = projection.toPixels(overLayItem.getPoint(), null);

// 可在此处添加您的绘制代码
   Paint paintText = new Paint();
   paintText.setColor(Color.BLUE);
   paintText.setTextSize(15);
   canvas.drawText(title, point.x - 30, point.y, paintText); // 绘制文本
  }

super.draw(canvas, mapView, shadow);
  // 调整一个drawable边界,使得(0,0)是这个drawable底部最后一行中心的一个像素
  // 先所有点的经度转像素
  ArrayList<Point> points = new ArrayList<Point>();
  for (int i = 0; i < this.size(); i++) {
   Point p = new Point();
   OverlayItem overLayItem = getItem(i);
   projection.toPixels(overLayItem.getPoint(), p);
   points.add(p);
  }
  // 第二个画笔 画线
  Paint paint = new Paint();
  paint.setColor(Color.BLUE);
  paint.setDither(true);
  paint.setStyle(Paint.Style.STROKE);
  paint.setStrokeJoin(Paint.Join.ROUND);
  paint.setStrokeCap(Paint.Cap.ROUND);
  paint.setStrokeWidth(4);

// 连接 所有点
  Path path = new Path();
  path.moveTo(points.get(0).x, points.get(0).y);
  for (int i = 1; i < points.size(); i++) {
   path.lineTo(points.get(i).x, points.get(i).y);
  }
  // 画出路径
  canvas.drawPath(path, paint);
  boundCenterBottom(marker);
 }

@Override
 protected OverlayItem createItem(int i) {
  // TODO Auto-generated method stub
  return mGeoList.get(i);
 }

@Override
 public int size() {
  // TODO Auto-generated method stub
  return mGeoList.size();
 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/e5f84d21eb3eb2a3ad213ac4e2dfde2e.html