zoukankan      html  css  js  c++  java
  • 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" />

    效果如下图:

  • 相关阅读:
    第二周课程进度
    Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)
    Codeforces Round #538 (Div. 2) C. Trailing Loves (or L'oeufs?) (分解质因数)
    Codeforces Round #162 (Div. 1) B. Good Sequences (dp+分解素数)
    Codeforces Round #552 (Div. 3) F. Shovels Shop (前缀和预处理+贪心+dp)
    桂林电子科技大学第三届ACM程序设计竞赛 G 路径
    Codeforces Round #551 (Div. 2) D. Serval and Rooted Tree (树形dp)
    Codeforces Round #324 (Div. 2) (哥德巴赫猜想)
    Codeforces Round #244 (Div. 2) C. Checkposts (tarjan 强连通分量)
    Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)
  • 原文地址:https://www.cnblogs.com/xuewater/p/2891371.html
Copyright © 2011-2022 走看看