在Android中的谷歌地图上添加多个标记

我是在谷歌地图上添加多个标记的triyng.这是我的代码部分
public class GoogleMap extends MapView 
 {
     MapController mc;
     MapView mapView; 
     GeoPoint p; 

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    ....

         double lat = Double.parseDouble(bundle.getString("paramLat"));
         double lng = Double.parseDouble(bundle.getString("paramLong"));

         mc = mapView.getController();
         p = new GeoPoint( (int) (lat * 1E6),(int) (lng * 1E6));
         mc.animateto(p);
         mc.setZoom(17);

         //---Add a location marker---
         MapOverlay mapOverlay = new MapOverlay();
         List<Overlay> listofOverlays = mapView.getoverlays();
         listofOverlays.clear();
         listofOverlays.add(mapOverlay);    

         mapView.invalidate();
  }




class MapOverlay extends com.google.android.maps.Overlay
    {

         @Override
         public boolean draw(Canvas canvas,MapView mapView,boolean shadow,long when) 
         {
            super.draw(canvas,mapView,shadow);       


                //---translate the GeoPoint to screen pixels---
                    Point screenPts = new Point();
                    mapView.getProjection().toPixels(p,screenPts);

                   //---add the marker---
                   Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.pushpin);            
                   canvas.drawBitmap(bmp,screenPts.x,screenPts.y-50,null);     


                   return true;



         }
}

我这里有两个问题.当我试图只添加一个标记时,它可以工作但是多次调用draw方法.但为什么?什么时候被调用

第二个问题是我如何添加标记.我创建了第二个名为p2的geoPoint,之后该怎么办?非常感谢你.

解决方法

我在我的项目中实现了多个标记.这是示例代码;您需要更改的一些内容标记图像,长度(您希望在for循环中定义的标记数).希望这会有所帮助!!!
public class ShowMapActivity extends MapActivity{
    private MapController mapControll;
    private GeoPoint geoPoint=null;
    private MapView mapview;
    private Myitemizedoverlay userPicOverlay;
    private Myitemizedoverlay nearPicOverlay;
    private Drawable userPic,atmPic;
    private OverlayItem nearatms[] = new OverlayItem[50];
    public static Context context;

    @Override
    protected void onCreate(Bundle icicle) {
        // Todo Auto-generated method stub
        super.onCreate(icicle);
        context = getApplicationContext();
        setContentView(R.layout.your layout xml);
        showMap();
    }

    public void showMap() {
        // Todo Auto-generated method stub

        try {
            geoPoint = new GeoPoint((int)(latitude * 1E6),(int)(longitude * 1E6));          
            mapview = (MapView)findViewById(R.id.mapview);
            mapControll= mapview.getController();
            mapview.setBuiltInZoomControls(true);
            mapview.setStreetView(true);
            mapControll.setZoom(16);
            mapControll.animateto(geoPoint);

            userPic = this.getResources().getDrawable(R.drawable.your pic);
            userPicOverlay = new Myitemizedoverlay(userPic);
            OverlayItem overlayItem = new OverlayItem(geoPoint,"I'm Here!!!",null);
            userPicOverlay.addOverlay(overlayItem);
            mapview.getoverlays().add(userPicOverlay);


            atmPic = this.getResources().getDrawable(R.drawable.your pic);
            nearPicOverlay = new Myitemizedoverlay(atmPic);
            for (int i = 0; i < define your length here; i++) {
                nearatms[i] = new OverlayItem(new GeoPoint((int)((latitude) * 1E6)),(int)(longitude) * 1E6)),"Name",null);//just check the brackets i just made change here so....
                nearPicOverlay.addOverlay(nearatms[i]);
            }
            mapview.getoverlays().add(nearPicOverlay);
            //Added symbols will be displayed when map is redrawn so force redraw Now
            mapview.postInvalidate();
        } catch (Exception e) {
            e.printstacktrace();
        }
    }

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

}

用于放置标记的分项类别

public class Myitemizedoverlay extends itemizedoverlay<OverlayItem> {

    private ArrayList<OverlayItem> myOverlays ;

    public Myitemizedoverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
        myOverlays = new ArrayList<OverlayItem>();
        populate();
    }

    public void addOverlay(OverlayItem overlay){
        myOverlays.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return myOverlays.get(i);
    }

    // Removes overlay item i
    public void removeItem(int i){
        myOverlays.remove(i);
        populate();
    }

    // Returns present number of items in list
    @Override
    public int size() {
        return myOverlays.size();
    }


    public void addOverlayItem(OverlayItem overlayItem) {
        myOverlays.add(overlayItem);
        populate();
    }


    public void addOverlayItem(int lat,int lon,String title) {
        try {
            GeoPoint point = new GeoPoint(lat,lon);
            OverlayItem overlayItem = new OverlayItem(point,title,null);
            addOverlayItem(overlayItem);    
        } catch (Exception e) {
            // Todo: handle exception
            e.printstacktrace();
        }
    }

    @Override
    protected boolean onTap(int index) {
        // Todo Auto-generated method stub
        String title = myOverlays.get(index).getTitle();
        Toast.makeText(ShowMapActivity.context,Toast.LENGTH_LONG).show();
        return super.onTap(index);
    }
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...