android百度地图多点绘制路线

现需要实现这样的功能:将给定的多个定位点进行连接,让其形成一条路线;点击每个定位点出现该点的基本信息。百度地图有现成的路线覆盖物,但现有的路线(公交、驾车、步行)都只是起点到终点,不能经过多点。只需要继承ItemizedOverlay类,实现其draw、ontap方法:

@Override
 public void draw(Canvas canvas, MapView mapView, boolean shadow) {
  super.draw(canvas, mapView, shadow);
  // Projection接口用于屏幕像素点坐标系统和地球表面经纬度点坐标系统之间的变换
  Projection projection = mapView.getProjection();

  // 遍历所有的OverlayItem
  for (int index = this.size() - 1; index >0; index--) {
   // 得到给定索引及后一个的item
   OverlayItem overLayItemstart = getItem(index);
   OverlayItem overLayItemstop = getItem(index-1);
   // 把经纬度变换到相对于MapView左上角的屏幕像素坐标
   Point pointfrom = projection.toPixels(overLayItemstart.getPoint(), null);
   Point pointto = projection.toPixels(overLayItemstop.getPoint(), null);
   Paint paintLine=new Paint();
   paintLine.setColor(Color.BLUE);
   paintLine.setStrokeWidth(5);
   canvas.drawLine(pointfrom.x, pointfrom.y, pointto.x, pointto.y, paintLine);//两点之间画直线
  }
  Paint paintText = new Paint();
  paintText.setColor(Color.BLUE);
  paintText.setTextSize(15);
  Point pointFirst = projection.toPixels(getItem(0).getPoint(), null);//路线的起点
  Point pointLast = projection.toPixels(getItem(this.size() - 1).getPoint(), null);//路线的终点
  canvas.drawText("起点", pointFirst.x, pointFirst.y, paintText);//添加起点终点标记
  canvas.drawText("终点", pointLast.x, pointLast.y, paintText);
 }

另外,实现onTab方法,实现定位点的点击操作,

@Override
 protected boolean onTap(int i) {
  // 处理当点击事件
  setFocus(mGeoList.get(i));
  GeoPoint pt = mGeoList.get(i).getPoint();
  baiduMap.updateViewLayout(popView,
    new MapView.LayoutParams(LayoutParams.WRAP_CONTENT,
      LayoutParams.WRAP_CONTENT, pt,
      MapView.LayoutParams.BOTTOM_CENTER));
  mContext.popView.setVisibility(View.VISIBLE);
  TextView TITLE = (TextView) findViewById(R.id.text_title);
  TITLE.setText("这是第"+i+"个定位点");
  // 关闭信息
  ImageView imageView = (ImageView) findViewById(R.id.ima_close);
  imageView.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    popView.setVisibility(View.GONE);
   }
  });
  return true;
 }

注:baiduMap是MapView控件,popView是点击后弹出信息的布局页面;popView布局代码如下:

http://schemas.android.com/apk/res/android"
 android:background="@drawable/info_bubble"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:paddingLeft="20px"
 android:paddingTop="10px"
 android:paddingRight="20px"
 android:paddingBottom="30px"   
   >
   
       android:ellipsize="marquee"
       android:layout_width="120px"
       android:layout_height="wrap_content"
       android:singleLine="true"   />
   
    android:background="@drawable/close"
    android:layout_width="30px"
    android:layout_toRightOf="@id/text_title"
       android:layout_height="wrap_content" />

效果如下图:

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注