本文介绍了对谷歌android系统中的地图添加多个标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我triyng添加多个标记在谷歌地图。这是我的code部分

I am triyng to add multiple marker on google map. Here is my code section

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;



         }
}

我有两个问题在这里。当我试图只添加一个标志,它的作品,但画法被调用很多次。但为什么?而当它被调用?

I have two question here. When i tried to add only one marker, it works but draw method is invoked many times. But why? and when is it invoked?

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

The second question is how can i add new marker. I created second geoPoint named p2 and after that what should i do? Thank you very much.

推荐答案

我已经实现了我的项目中多个标记。下面是示例code;有些事情,你需要改变的是(你要定义在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, title, Toast.LENGTH_LONG).show();
        return super.onTap(index);
    }
}

这篇关于对谷歌android系统中的地图添加多个标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 16:25