最近的项目要用到百度地图,并且地图上的图层还要有动画效果,于是对此方面研究了一下:
首先考虑使用OverlayItem,发现这是一个继承自Object的对象,呃...,那算了
接着考虑添加View的方式,因为百度地图的MapView本身也是继承于ViewGroup的,这里要注意的一个问题就是要把地图坐标转换成ViewGroup中的位置,于是
MapView mv=new MapView(context); GeoPoint location=new GeoPoint(0,0)//测试 //获取子控件在父控件中的位置信息 Projection projection = mv.getProjection(); Point point = new Point(); projection.toPixels(location, point); Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(), R.drawable.ex_ping_user); int marginLeft = point.x - bitmap.getWidth() / 2; int marginTop = point.y - bitmap.getHeight(); //将子控件放到RelativeLayout的父控件中,然后将此RelativeLayout放入MapView RelativeLayout parent=new RelativeLayout(this); parent.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT)); ImageView iv=new ImageView(context); Animation anim=AnimationUtils.loadAnimation(context, R.anim.ex_map_popup); iv.setImageResource(R.drawable.ex_ping_user); iv.setAnimation(anim); RelativeLayout.LayoutParams ivLp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT); ivLp.setMargins(marginLeft, marginTop, 0, 0); iv.setLayoutParams(ivLp); parent.addView(iv); parent.requestLayout(); mv.addView(parent); mv.refresh();
再看看官方文档吧,ok,原来官方提供了这种方法,所以修改后如下
MapView mv=new MapView(this); GeoPoint location=new GeoPoint(0,0);//换成你的坐标 MapView.LayoutParams mapviewLp = new MapView.LayoutParams( MapView.LayoutParams.WRAP_CONTENT,MapView.LayoutParams.WRAP_CONTENT, location,MapView.LayoutParams.BOTTOM_CENTER); ImageView iv=new ImageView(context); Animation anim=AnimationUtils.loadAnimation(context, R.anim.ex_map_popup); iv.setImageResource(R.drawable.ex_ping_user); iv.setAnimation(anim); mv.addView(iv,mapviewLp); mv.refresh();