Search code examples
androidgoogle-mapsgoogle-maps-markersgoogle-maps-mobile

Get Bounds of a custom map marker in android


I have set of markers on my map view. I want to get the bounds of a map marker so that I can detect which map marker I tapped. I have searched in stackoverflow but all are telling how to set the bounds of a map marker(drawable). But what I want is getting the bounds of the marker that I have drawn already.

Please help

Thank You


Solution

  • This link leads to project I advise not to copy but to take inspiration from. Look at which methods are used for what and maybe you can find some good ideas there. I don't recommend copying for your own reason - to learn while programming(you will not learn anything if you simply copy).

    MapOverlay is little more complicated in the Android API than it should be, I admit, but if you take a closer look, you will get into it pretty quickly. I recommend starting here, for to me unknown reason Google separated this as external library and is no longer available on old developer page. Take a close look at Itemized Overlay, which I am using for drawing objects on map. You will need to extend this class and override few methods. As you can see, it already has onTap method, which you simply override as well:)

    To make sure you understand what I mean - here is my own Itemized Overlay:

    package example;
    
    import java.util.ArrayList;
    import java.util.concurrent.ExecutionException;
    
    import android.app.AlertDialog;
    import android.app.ProgressDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.res.Resources.NotFoundException;
    import android.graphics.drawable.Drawable;
    import android.os.AsyncTask;
    
    import com.google.android.maps.ItemizedOverlay;
    import com.google.android.maps.OverlayItem;
    /**
     * Paints and stores all the Overlays for CurrentPositionActivity, overrides ItemizedOverlay<OvelayItem>.
     * @author Michal Svacha
     * 
     */
     public class ItemizedOverlayBeta extends ItemizedOverlay<OverlayItem> {
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); // ArrayList for the OverlayItems
    Context mContext; // Context where the Overlays are to eb displayed
    private ArrayList<String> mObjIDs = new ArrayList<String>();
    DetailLoader dl;
    
    public void addID(String objId) {
        mObjIDs.add(objId);
    }
    
    /**
     * Public constructor.
     * @param defaultMarker
     * @param context
     */
    public ItemizedOverlayBeta(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker)); // Calls the constructor of the parent.
        mContext = context;
    }
    
    /**
     * Adds overlay to ArrayList.
     * @param overlay
     */
    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }
    
    /**
     * Returns OverlayItem based on the index.
     * @param index - position where the OverlayItem is stored in the ArrayList.
     * @return desired OverlayItem.
     */
    @Override
    protected OverlayItem createItem(int index) {
      return mOverlays.get(index);
    }
    
    /**
     * Returns the size of the ArrayList
     * @return calls method size() on the ArrayList
     */
    @Override
    public int size() {
      return mOverlays.size();
    }
    
    /**
     * When tapped on screen this method is called and creates AlertDialog with little data.
     * @param index - place where tapped
     * @return always true
     */
    @Override
    protected boolean onTap(int index) {
      dl = new DetailLoader();
      try {
        if(dl.execute(index).get()) {
            OverlayItem item = mOverlays.get(index);
            final AlertDialog dialog = new AlertDialog.Builder(mContext).create();
            dialog.setTitle(item.getTitle());
            dialog.setMessage(item.getSnippet() +"\n" + "Hello");
            dialog.setButton("close", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
          });
          dialog.show();
          }
    } catch (NotFoundException e) {
        return false;
    } catch (InterruptedException e) {
        return false;
    } catch (ExecutionException e) {
        return false;
    }
      return true;
    }
    
    public void populateNow(){
        populate();
    }
    
    private class DetailLoader extends AsyncTask<Integer, Void, Boolean>{
        ProgressDialog pd;
    
        @Override
        protected void onPreExecute() {
            pd = new ProgressDialog(mContext);
            pd = ProgressDialog.show(mContext, "","Loading",true);
        }
    
        @Override
        protected void onPostExecute(Boolean result) {
            if(pd != null) pd.dismiss();
        }
    
        @Override
        protected Boolean doInBackground(Integer... params) {
            // Load data
            return true;
        }
    }
    }
    

    Which is then displayed on the map like this:

    ItemizedOverlayBeta itemizedMoving = new ItemizedOverlayBeta(this.getResources().getDrawable(R.drawable.ic_pin_moving), this);
    GeoPoint gp = new GeoPoint((int)(coordinate * 1E6),(int)(coordinate2 * 1E6));
    OverlayItem oi = new OverlayItem(gp, "string for the alert dialog";    
    itemizedMoving.populateNow();
    CurrentPositionActivity.mapOverlays.add(itemizedMoving);
    

    So basically when you need some popover over the pin, you need to implement it IN the overlay, not the map, because, you tell the pin - hey, when I click you, you need to redraw yourself, just like I am displaying an alert dialog.

    Hope it helps!