views:

259

answers:

3

I created a custom OverlayItem class so that I could essentially have one kind of OverlayItem whose Drawable marker would set itself depending on the state of some data that I pass into it.

I have attempted to accomplish this by, on my first attempt, utilizing the setMarker method within the OverlayItem class. Once that did not work I attempt to override the getMarker method and have it return the appropriate marker to represent the data.

Both of these attempts ended with nothing being drawn on the map...however if they are commented out the markers draw just fine (except they of course use the default marker, which isn't what I want).

Here is my code for my custom OverlayItem class (the commented out methods I have tried and they have not worked):

private class MyOverlayItem extends OverlayItem {
    private Context mContext;
    private MyData mData;

    public MyOverlayItem(GeoPoint point, MyData data, Context context) {
        super(point, data.getWhat(), data.getWhere());
        this.mContext = context;
        this.mData = data;

        /*if(data.getTemp() > 200)
            this.setMarker(res.getDrawable(R.drawable.icon_data_hot_l));
        else if(data.getTemp() > 100)
            this.setMarker(res.getDrawable(R.drawable.icon_data_neutral_l));
        else
            this.setMarker(res.getDrawable(R.drawable.icon_data_frozen_l));*/
    }

    /*@Override
    public Drawable getMarker(int stateBitset) {
        Resources res = this.mContext.getResources();
        if(this.mData.getTemp() > 200)
            return res.getDrawable(R.drawable.icon_data_hot_l);
        else if(this.mData.getTemp() > 100)
            return res.getDrawable(R.drawable.icon_data_neutral_l);
        return res.getDrawable(R.drawable.icon_data_frozen_l);
    }*/
}

Is there a way to do what I am attempting to do...or do I need to make a unique OverlayItem class corresponding to each state of my data? (Ew.)

A: 

Try overriding the onDraw() method of your overlay class.

Soumya Simanta
Do you mean the draw(...) method? That seems as if it would be too complicated and too much of a workaround.The documentation states that, "An item may provide an alternate marker via its OverlayItem.getMarker(int) method. If that method returns null, the default marker is used," so shouldn't I be able to override the getMarker method and have it draw the different markers?
celestialorb
A: 

I have been able to make the markers appear, however they are appearing upside-down (at least, I think they are, it may just be that their shadow is at the top-left rather than the bottom-right).

All I needed to do was add this line:

this.mMarker.setBounds(0, 0, this.mMarker.getIntrinsicWidth(), this.mMarker.getIntrinsicHeight());

...as well as this line to correctly orient the markers (I added this when my ItemizedOverlay adds tan overlay item:

overlay.setMarker(boundCenterBottom(overlay.getMarker(0)));
celestialorb
+1  A: 

The API states that the Marker needs bounds to be displayed...

@celestialorb Either one of your snippets does the job alone (or mine worked with either anyway) when you use setBounds() alone, you get what you tell it to do... the bounding box for your marker is set. The orientation to the OverlayItem's Point and shadow state of the Marker, etc are set to default (didn't look those up, but they don't do what I want...) :)

mOverlay.setMarker(boundCenterBottom(mOverlay.getMarker(0))); does the complete job for you, setting the bounds and centering the bottom of the Marker on the Point (hence the stylish Method name).

Now, as to best practices, Brighter bulbs than I will have to chime in...

Peace, Dan

Dan