Android 百度地图API-----气泡显示

一个小例子,在地图中显示一个浮标,显示出我们的标记点,点击这个浮标会出现一个文本显示的气泡

先来看看效果:


public class MainActivity extends MapActivity {  BMapManager mBMapMan;   MapController mMapController;   MKOfflineMap mOffline = null; //申明变量 离线地图   static View mPopView = null; // 点击mark时弹出的气泡View   MyOverItemT overitem = null;   MapView mMapView;     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         GeoPoint point =  new GeoPoint((int) ( 22.560034 * 1E6),(int) (113.940657 * 1E6));         List<GeoPoint> lgp=new ArrayList<GeoPoint>();         lgp.add(point);           //BMapManager的注册和初始化         mBMapMan = new BMapManager(getApplication());         mBMapMan.init("3B388F2271D0255A779274772B906055B2942035", null);         mBMapMan.start();         super.initMapActivity(mBMapMan);         //mapview地图控件的设置         mMapView = (MapView) findViewById(R.id.bmapsView);         mMapView.setBuiltInZoomControls(true); //         mMapView.setDrawOverlayWhenZooming(true);//在地图缩放的过程里绘制         mMapView.getController().setCenter(point);//将指定点作为地图的中心显示出来                         //标记         Drawable marker = getResources().getDrawable(R.drawable.iconmarka);         //为maker定义位置和边界         marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());                   //这是在点击了标记之后,弹出的浮动窗口   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);                           overitem=new MyOverItemT(marker,this,lgp);         mMapView.getOverlays().add(overitem);     }     @Override     public boolean onCreateOptionsMenu(Menu menu) {         getMenuInflater().inflate(R.menu.activity_main, menu);         return true;     }     @Override     protected void onDestroy() {         if (mBMapMan != null) {             mBMapMan.destroy();             mBMapMan = null;         }         super.onDestroy();     }     @Override     protected void onPause() {         if (mBMapMan != null) {             mBMapMan.stop();         }         super.onPause();     }     @Override     protected void onResume() {         if (mBMapMan != null) {             mBMapMan.start();         }         super.onResume();     }

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

自定义图层类:

class MyOverItemT extends ItemizedOverlay<OverlayItem> {  public List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();  private Drawable marker;//标记  private MainActivity ma;//上下文  public List<GeoPoint> gpl = new ArrayList<GeoPoint>();  Projection projection;  //初始化,生成图层的item。  public MyOverItemT(Drawable marker, MainActivity context, List<GeoPoint> gplist) {       super(boundCenterBottom(marker));             this.marker=marker;          this.ma=context;         //根据gplist来制作OverlayItem          for(int i=0;i<gplist.size();i++){           GeoPoint gp=(GeoPoint)gplist.get(i);           OverlayItem ot=new OverlayItem(gp,"gp"+i,"gpoint"+i);           mGeoList.add(ot);          }       populate();//更新  }  //核心的方法  1 draw:将图层上的item绘制出来    2 onTap:触发每个item的时候响应动作  @Override  public void draw(Canvas canvas, MapView mapView, boolean shadow) {     //将List<OverlayItem> mGeoList 全部绘制出来   // Projection接口用于屏幕像素坐标和经纬度坐标之间的变换     projection = mapView.getProjection();     //得到OverlayItem,然后转换成point点     //然后根据这个点绘制     for (int index = size() - 1; index >= 0; index--) {      OverlayItem overLayItem = getItem(index);      String title = overLayItem.getTitle();      Point point = projection.toPixels(overLayItem.getPoint(), null);      Paint paintText = new Paint();      paintText.setColor(Color.RED);      paintText.setTextSize(15);      canvas.drawText(title, point.x-30, point.y, paintText); // 绘制文本     }     super.draw(canvas, mapView, shadow);     boundCenterBottom(marker);  }  @Override  public boolean onTap(GeoPoint arg0, MapView arg1) {   //浮标消失   ma.mPopView.setVisibility(View.GONE);   return super.onTap(arg0, arg1);  }  @Override  protected boolean onTap(int i) {   //首先 某一个item获得焦点   //然后将标记图标的view 加入到mapview中   setFocus(mGeoList.get(i));   GeoPoint pt = mGeoList.get(i).getPoint();   Point pn=projection.toPixels(pt, null);   ma.mMapView.updateViewLayout( ma.mPopView,                 new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,                   pt, MapView.LayoutParams.BOTTOM_CENTER));   ma.mPopView.setVisibility(View.VISIBLE);      //得到坐标然后显示出来   TextView tv1=(TextView)ma.mMapView.findViewById(R.id.tv1);   tv1.setText("经纬度:"+pt.getLongitudeE6()/1e6+","+pt.getLatitudeE6()/1e6);   TextView tv2=(TextView)ma.mMapView.findViewById(R.id.tv2);   tv2.setText("坐标:"+pn.x+","+pn.y);   return true;  }  @Override   protected OverlayItem createItem(int arg0) {   // TODO Auto-generated method stub   return mGeoList.get(arg0);  }

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

图标的布局样式:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="horizontal"     android:paddingBottom="25dip" >

    <LinearLayout         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:background="@drawable/pop"         android:orientation="vertical" >

        <TextView             android:id="@+id/tv1"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="坐标" />

        <TextView             android:id="@+id/tv2"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="x,y" />     </LinearLayout>

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值