Tuesday, March 16, 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;
    }
}



13 comments:

  1. can we have zip file for full source code?

    ReplyDelete
  2. Excellent content ,Thanks for sharing this .,
    Leanpitch provides online training in ICP ACC, everyone can use it wisely.
    ICP ACC certification online
    Agile coach certification

    ReplyDelete
  3. Market Forex Is The Best Place To Research And Compare Brokerages. We've Worked Hard To Find You The Best Brokers In Terms Of Commissions, Minimum Deposit, And Account Restrictions. Our Mission Is To Make It Easier For The Average Investor To Find, Compare And Review The Best Brokers On Their Own Time.

    ReplyDelete
  4. Live Seacoin Price from all markets and SEA coin market Capitalization. Stay up to date with the latest SEA price movements and forum discussion. Check out our snapshot charts and see when there is an opportunity to buy or sell.

    ReplyDelete
  5. The Truff Stock Overview, keep track of all your favorite stocks in real time across multiple screens, tailored to only show information you need and see as it happens.

    ReplyDelete
  6. We understand that standing on the sidelines of the stock market can be frustrating. Our live, real stock market overview will help you stay up to date on Truff Stock . With our simple interface and real time quotes, staying on top of your stocks is now easier than ever!

    ReplyDelete
  7. Bfarf stock Overview: Stay ahead of the market with our live and real time stock market overview. Get all of your favorite stocks in one place, and get alerted to live news at the same time!

    ReplyDelete
  8. Would You Like To Access Your TD Ameritrade Review Account And Manage Your Trades? This Is The Place For You. Here You Can Easily Manage Your Investments, Deposit Or Withdraw Funds. Read More Here.

    ReplyDelete
  9. Read our honest broker review of TD Ameritrade Review , one of the best online trading companies. Find out about the advantages of using a broker like TD Ameritrade Review, and learn more about the stock market and the best ways to invest in it. Read more here.

    ReplyDelete
  10. AFM Logistics Pvt Ltd is Best Air Import Freight Forwarders In India established in Delhi. The company was constituted in 2012 and is indulged in providing complete logistics solution. The company has its own setup and wide network of agents throughout the world. Import and Export company in India. They are the Top Freight Forwarding Companies In India. AFM Logistics Pvt Ltd has been working as Best Custom Clearing Company in Delhi since 2012. They have been the Best Air Cargo Company in Delhi for a very long time.

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete