Slideshow

Friday, April 6, 2012

Adding Multiple Markers on Android Map View

package com.AndroidMapDemo;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MapDemoActivity extends MapActivity {
    /** Called when the activity is first created. */
    MapView mv;
   
    MapController mc;
    GeoPoint gp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mv=(MapView) findViewById(R.id.myMapView);
        mv.setBuiltInZoomControls(true);
        mc= mv.getController();       
        mc.setZoom(16);
        mc.animateTo(new GeoPoint(22719467,75853043));

       
        Drawable marker=getResources().getDrawable(android.R.drawable.star_on);
        int markerWidth = marker.getIntrinsicWidth();
        int markerHeight = marker.getIntrinsicHeight();
        marker.setBounds(0, markerHeight, markerWidth, 0);
         
        MyItemizedOverlay mio = new MyItemizedOverlay(marker);
        mv.getOverlays().add(mio);
      
        mio.addItem(new GeoPoint(22719467,75853043),
    "KB", "Khajuri Bazar");
        mio.addItem(new GeoPoint(22721486,75859823),
    "NG",
    "Nagar Nigam");
        mio.addItem(new GeoPoint(   22718399,75854759),
                "RJ",
                "Rajwada");
    
    }
    

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

class MyItemizedOverlay extends ItemizedOverlay
{
     private List items=new ArrayList();
   
   
    public MyItemizedOverlay(Drawable defaultMarker) {
        super(defaultMarker);
       
        // TODO Auto-generated constructor stub
        boundCenterBottom(defaultMarker);
   

          populate();

    }
    public void addItem(GeoPoint p, String title, String snippet){
        OverlayItem newItem = new OverlayItem(p, title, snippet);
        items.add(newItem);
           populate();
        }
    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return(items.get(i));

    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return(items.size());
    }
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        // TODO Auto-generated method stub
        super.draw(canvas, mapView, shadow);
       
       
        }
     @Override
        protected boolean onTap(int i) {
          Toast.makeText(MapDemoActivity.this,
                          items.get(i).getSnippet(),
                          Toast.LENGTH_SHORT).show();
         
          return(true);
        }
}
}

1 comments:

sarath Nk said...

gooooooooooooooooooooooood

Post a Comment