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



14 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. Don't be fooled by your SEO strategy. It's important to have a strategy in place, but don't think that this is all you need to do. You also need to make sure that you're getting your site in front of the right people. Do you have a website that needs a little more exposure? We offer Fx Seo that will increase your rankings and get you in front of the right people. We offer a variety of Fx Seo packages that will be tailored to your needs. We'll create a plan for you, so you can see how it works before you buy. What are you waiting for? Get your site in front of the right people with our Fx Seo today!

    ReplyDelete
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. At Tradingzy, we can help you learn What Is Liquidity In Cryptocurrency and how to use it. We're a digital marketing agency focusing on SEO for the Forex Brokerage company. We're familiar with Google's updates and how to work with them effectively.

    ReplyDelete
  10. 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
  11. 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
  12. 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
  13. This comment has been removed by the author.

    ReplyDelete