Selasa, 16 Maret 2010

Steps to place the Marker in the Map [Overlay Example]

In this ariticle used to place the marker in the map view.
1. Create a Customize Marker Class
Class Should Extend the ItemizedOverlay
Create a parameterized Constructor. i am trying with two constructor.
    The constructor must define the default marker for each of the OverlayItems. In order for the Drawable to actually get drawn, it must have its bounds defined.

    Most commonly, you want the center-point at the bottom of the image to be the point at which it's attached to the map coordinates. This is handled for you with the boundCenterBottom() method. Wrap this around our defaultMarker, so the super constructor call looks like this:

    public HelloItemizedOverlay(Drawable defaultMarker) {
      super(boundCenterBottom(defaultMarker));
    }


    public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
      super(defaultMarker);
      mContext = context;
    }

2. Create the following class members
    1.  private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
        Used to put each of the OverlayItem objects we want on the map.
    2. private Context mContext ;

    3. In order to add new OverlayItems to the ArrayList, you need a new method:

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

        Now define the HelloItemizedOverlay constructors. The constructor must define the default marker for each of the OverlayItems. In order for the Drawable to actually get drawn, it must have its bounds defined.

3. Override the following methods

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

@Override
public int size() {
  return mOverlays.size();
}

@Override
protected boolean onTap(int index) {
    OverlayItem item = mOverlays.get(index);
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
    dialog.setTitle(item.getTitle());
    dialog.setMessage(item.getSnippet());
    dialog.setPositiveButton("Yes", new OnClickListener() {
       
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    dialog.show();
    return true;
}

4. Implement the Marker in the map activity
    1. Get the Overlay as a list from mapview
        List<Overlay> mapOverlays = myMapView.getOverlays()

    2. Instantiate the following class
        Drawable with marker icon
            Drawable drawable = this.getResources().getDrawable(R.drawable.pinicon);

        Marker with Drawable object
            MyMapMarker itemizedoverlay = new MyMapMarker(drawable,this);

        OverlayItem with geopoint
            OverlayItem overlayitem = new OverlayItem(geoPoint, "Info",
                    "Selected City is" + city);

   
    3. All that's left is to add this OverlayItem to your collection, then add the MainActivity to the MapView:
        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);


Sample Source

public class MyMapMarker extends ItemizedOverlay {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private Context mContext;

   
    public MyMapMarker(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
        // TODO Auto-generated constructor stub
    }

    public MyMapMarker(Drawable defaultMarker, Context context) {
        this(defaultMarker);
        mContext = context;
    }

    public void addOverlay(OverlayItem item) {
        mOverlays.add(item);
        populate();

    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }
    @Override
    public int size() {
        return mOverlays.size();
    }
    @Override
    protected boolean onTap(int index) {
        OverlayItem item = mOverlays.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.setPositiveButton("Yes", new OnClickListener() {   
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.show();
        return true;
    }
}



Tidak ada komentar:

Posting Komentar