现需要实现这样的功能:将给定的多个定位点进行连接,让其形成一条路线;点击每个定位点出现该点的基本信息。百度地图有现成的路线覆盖物,但现有的路线(公交、驾车、步行)都只是起点到终点,不能经过多点。只需要继承ItemizedOverlay类,实现其draw、ontap方法:
@Override public void drawCanvas canvas, MapView mapView, boolean shadow) { super.drawcanvas, mapView, shadow); // Projection接口用于屏幕像素点坐标系统和地球表面经纬度点坐标系统之间的变换 Projection projection = mapView.getProjection); // 遍历所有的OverlayItem for int index = this.size) - 1; index >0; index--) { // 得到给定索引及后一个的item OverlayItem overLayItemstart = getItemindex); OverlayItem overLayItemstop = getItemindex-1); // 把经纬度变换到相对于MapView左上角的屏幕像素坐标 Point pointfrom = projection.toPixelsoverLayItemstart.getPoint), null); Point pointto = projection.toPixelsoverLayItemstop.getPoint), null); Paint paintLine=new Paint); paintLine.setColorColor.BLUE); paintLine.setStrokeWidth5); canvas.drawLinepointfrom.x, pointfrom.y, pointto.x, pointto.y, paintLine);//两点之间画直线 } Paint paintText = new Paint); paintText.setColorColor.BLUE); paintText.setTextSize15); Point pointFirst = projection.toPixelsgetItem0).getPoint), null);//路线的起点 Point pointLast = projection.toPixelsgetItemthis.size) - 1).getPoint), null);//路线的终点 canvas.drawText"起点", pointFirst.x, pointFirst.y, paintText);//添加起点终点标记 canvas.drawText"终点", pointLast.x, pointLast.y, paintText); }
另外,实现onTab方法,实现定位点的点击操作,
@Override protected boolean onTapint i) { // 处理当点击事件 setFocusmGeoList.geti)); GeoPoint pt = mGeoList.geti).getPoint); baiduMap.updateViewLayoutpopView, new MapView.LayoutParamsLayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, pt, MapView.LayoutParams.BOTTOM_CENTER)); mContext.popView.setVisibilityView.VISIBLE); TextView TITLE = TextView) findViewByIdR.id.text_title); TITLE.setText"这是第"+i+"个定位点"); // 关闭信息 ImageView imageView = ImageView) findViewByIdR.id.ima_close); imageView.setOnClickListenernew View.OnClickListener) { public void onClickView v) { popView.setVisibilityView.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" />
效果如下图: