简体   繁体   中英

How to create a custom dialog for a itemized overlay item?

So at the moment when i click an itemized overlay item , i just display a simple dialog, i would like to create something more in the lines of the below

点按对话框

How would i go about doing this for the android phone>?

When you override ItemizedOverlay there is protected onTap method. It has item index as parameter. You should override onTap and use this index to get correct data object. Eg:

@Override
protected boolean onTap(int index) {
    getItem(index); \\your overlay item
    return true;
}

Then you should create custom dialog from code or inflate xml layout, use correct layout params to add it on your MapView:

MapView.LayoutParams params = new MapView.LayoutParams(LayoutParams.WRAP_CONTENT,     
    LayoutParams.WRAP_CONTENT, point, 0, 0, MapView.LayoutParams.BOTTOM_CENTER);
    params.mode = MapView.LayoutParams.MODE_MAP;            
    MV.addView(popup, params);

MV - your MapView instance; point - your Overlay item GeoPoint; popup - your custom dialog view.

I just found a method to do that: Extend OverlayItem and override getMarker to return a new drawable.

package this.is.funny;

import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.graphics.drawable.NinePatchDrawable;

import com.google.android.maps.GeoPoint; import com.google.android.maps.MapView; import com.google.android.maps.OverlayItem;

public class MyOverlayItem extends OverlayItem{ private String title; private Context ctx; private NinePatchDrawable npd; private Rect textBox; private Paint p;

public MyOverlayItem(Context ctx,GeoPoint point, String title, String snippet) {
    super(point, title, snippet);
    this.title=title;
    this.ctx=ctx;
    p=new Paint();

}


@Override
public Drawable getMarker(int stateBitset) {
    return new MyDrawable();
}
/**
 * 
 * I believe this method is the cleaniest way to do what we want. Not sure about it
 * @author emmanuel
 * This private class is a custom drawable with some text inside.
 */
private class MyDrawable extends Drawable{

    @Override
    public void draw(Canvas c) {            

        textBox = new Rect();
                    //this is ugly
        p.setFakeBoldText(true);
        p.setTextSize(16);
        p.setAlpha(50);
        p.getTextBounds(title, 0, title.length(), textBox);
        //p.setAlpha(255);
        NinePatchDrawable npd;
        npd=(NinePatchDrawable) 

ctx.getResources().getDrawable(R.drawable.geoloc);

npd.setBounds(-(textBox.width()/2+15),-(textBox.height()+30),textBox.width()/2+15,0); npd.draw(c); p.setColor(Color.WHITE); p.setAlpha(255); c.drawText(title,-textBox.width()/2,-30,p); } @Override public int getOpacity() { return PixelFormat.TRANSLUCENT; } @Override public void setAlpha(int alpha) { // TODO Auto-generated method stub } @Override public void setColorFilter(ColorFilter cf) { // TODO Auto-generated method stub } }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM