package com.android.angking.yibai; import java.util.ArrayList; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Point; import android.graphics.drawable.Drawable; import com.google.android.maps.ItemizedOverlay; import com.google.android.maps.MapView; import com.google.android.maps.OverlayItem; import com.google.android.maps.Projection; public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem>{ private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); private Context context; public CustomItemizedOverlay(Context context,Drawable defaultMarker){ super(boundCenterBottom(defaultMarker)); this.context = context; } 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 overlayItem = getItem(index); //把经纬度变换相对于MapView左上角的屏幕像素坐标 Point point = projection.toPixels(overlayItem.getPoint(), null); Paint paintText = new Paint(); paintText.setColor(Color.RED); paintText.setTextSize(13); //绘制文本 canvas.drawText(overlayItem.getTitle(), point.x+10, point.y-15, paintText); } } protected boolean onTap(int index){ setFocus(mOverlays.get(index)); return super.onTap(index); } public void removeAll(){ if(mOverlays.size()>=0){ mOverlays.removeAll(mOverlays); } } public void addOverlay(OverlayItem overlay){ mOverlays.add(overlay); populate(); } // public CustomItemizedOverlay(Drawable defaultMarker) { // super(boundCenterBottom(defaultMarker)); // // TODO Auto-generated constructor stub // } @Override protected OverlayItem createItem(int i) { // TODO Auto-generated method stub return mOverlays.get(i); } @Override public int size() { // TODO Auto-generated method stub return mOverlays.size(); } }
自定义OverlayItem
package com.android.yibai.antking; import android.graphics.Bitmap; import com.google.android.maps.GeoPoint; import com.google.android.maps.OverlayItem; public class MyOverlayItem extends OverlayItem{ private Bitmap bitmap; public MyOverlayItem(GeoPoint point, String title, String snippet,Bitmap bitmap) { super(point, title, snippet); this.bitmap = bitmap; // TODO Auto-generated constructor stub } public Bitmap getBitmap(){ return bitmap; } }
实现类
package com.android.yibai.antking; import java.util.List; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import com.google.android.maps.GeoPoint; import com.google.android.maps.ItemizedOverlay; import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; import com.google.android.maps.OverlayItem; public class MapMain extends MapActivity{ /** * 地图 */ protected MapView mapView; /** * 弹出的气泡View */ private View popView; private int[] image={R.drawable.icon}; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //初始化气泡,并设置为不可见 popView = View.inflate(this, R.layout.popview, null); setContentView(R.layout.main); //获得map mapView = (MapView)this.findViewById(R.id.mapview); mapView.addView(popView,new MapView.LayoutParams( MapView.LayoutParams.WRAP_CONTENT, MapView.LayoutParams.WRAP_CONTENT,null ,MapView.LayoutParams.BOTTOM_CENTER)); //这里没有给GeoPoint ,在onFoucusChangeListener中设置 popView.setVisibility(View.GONE); /** * 创建图标资料(用于显示在overlayItem所表示表记的地位 */ Drawable drawable =this.getResources().getDrawable(R.drawable.icon); //为mark定以地位和鸿沟 drawable.setBounds(0,0,drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); CustomItemizedOverlay overlay = new CustomItemizedOverlay(this,drawable); //设置显示/隐藏气泡的位置 overlay.setOnFocusChangeListener(onFocusChangeListener); /** * 创建并添加一个标志 */ GeoPoint point = new GeoPoint(35422006,119524095); //创建标识 Bitmap bitmap = BitmapFactory.decodeResource(MapMain.this.getResources(), R.drawable.psu); MyOverlayItem overlayItem = new MyOverlayItem(point ,"银河公园","这是一个充满神奇的公园,一步一景,我去的时候有一种柳暗花明又一村的感觉",bitmap); overlay.addOverlay(overlayItem); /** * 创建第二个标识 */ Bitmap bitmap1 = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon); GeoPoint point1 = new GeoPoint((int)(22.53108*1E6),(int)(113.99151*1E6)); MyOverlayItem overlayItem1 = new MyOverlayItem(point1,"秀丽中华","中国最好的旅游胜地之一",bitmap1); overlay.addOverlay(overlayItem1); //向地图里添加自定义的ItemizedOverlay List<Overlay> mapOverlays =mapView.getOverlays(); mapOverlays.add(overlay); //设置地图为卫星地图 mapView.setSatellite(true); //设置地图可以缩放 mapView.setBuiltInZoomControls(true); /** * 取得地图管理对象,用于把握地图 * */ //设置地图的中间 mapView.getController().setCenter(point); //设置地图默认的缩放级别 mapView.getController().setZoom(13); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } private final ItemizedOverlay.OnFocusChangeListener onFocusChangeListener = new ItemizedOverlay.OnFocusChangeListener() { @Override public void onFocusChanged(ItemizedOverlay overlay, OverlayItem newFocus) { // TODO Auto-generated method stub //创建气泡窗口 if(popView!=null){ popView.setVisibility(View.GONE); } if(newFocus !=null){ MapView.LayoutParams geoLp =(MapView.LayoutParams) popView .getLayoutParams(); geoLp.point = newFocus.getPoint();//这行用于popView的定位 TextView title = (TextView)popView.findViewById(R.id.map_bubbleTitle); title.setText(newFocus.getTitle()); TextView desc = (TextView)popView.findViewById(R.id.map_bubbleText); ImageView image = (ImageView)popView.findViewById(R.id.map_bubbleImage); image.setImageBitmap(((MyOverlayItem) newFocus).getBitmap()); if(newFocus.getSnippet()==null ||newFocus.getSnippet().length()==0){ desc.setVisibility(View.GONE); }else{ desc.setVisibility(View.VISIBLE); desc.setText(newFocus.getSnippet()); } mapView.updateViewLayout(popView,geoLp); popView.setVisibility(View.VISIBLE); } } }; }