adding multiple marker on google map in android

余生长醉 提交于 2019-11-28 06:38:13

I have implemented the multiple markers in my project. Here is the sample code; some things that you need to change is the marker image, the length (number of marker you want define in the for loop). Hope this will help!!!

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;
    }

}

Itemized Class for placing the marker

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);
    }
}
Plamen Nikolov
  1. To prevent the multiple drawing you need a cache. This is a bug in the draw method of MapOverlay
  2. To add multiple markers you have to use ItemizedOverlay. This may help you.

You should follow the Android Map View tutorial on the developers site.

Part 2 has the section for building an Overlay.

http://developer.android.com/resources/tutorials/views/hello-mapview.html

Minimal work should be done in the Draw method; it is called a lot including everytime the map is moved/zoomed/"invalidated"

Your going to want to start with an ItemizedOverlay which is an array of points. You can find the documentation here http://code.google.com/android/add-ons/google-apis/reference/index.html . Then your going to want to invoke the ItemizedOverlay.draw() method which will draw all the points within it based on their position. Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!