使用这个类:
import java.util.ArrayList;
import org.mabna.order.utils.Farsi;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class MapItemizedOverlay extends ItemizedOverlay<OverlayItem> {
// member variables
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
private int markerHeight;
private int mTextSize;
private int mTitleMargin;
private Typeface tf;
private boolean mDrawCircle;
private int mRadiusInMeter = 0;
public MapItemizedOverlay(Drawable defaultMarker, Context context,
int textSize, int titleMargin, boolean drawCircle) {
super(boundCenterBottom(defaultMarker));
mContext = context;
mTextSize = textSize;
markerHeight = ((BitmapDrawable) defaultMarker).getBitmap().getHeight();
mTitleMargin = titleMargin;
mDrawCircle = drawCircle;
}
// In order for the populate() method to read each OverlayItem, it will make
// a request to createItem(int)
// define this method to properly read from our ArrayList
@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);
// Do stuff here when you tap, i.e. :
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
// return true to indicate we've taken care of it
return true;
}
@Override
public void draw(android.graphics.Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, shadow);
// go through all OverlayItems and draw title for each of them
for (OverlayItem item : mOverlays) {
/*
* Converts latitude & longitude of this overlay item to coordinates
* on screen. As we have called boundCenterBottom() in constructor,
* so these coordinates will be of the bottom center position of the
* displayed marker.
*/
GeoPoint point = item.getPoint();
Point markerBottomCenterCoords = new Point();
mapView.getProjection().toPixels(point, markerBottomCenterCoords);
/* Find the width and height of the title */
TextPaint paintText = new TextPaint();
Paint paintRect = new Paint();
Rect rectText = new Rect();
paintText.setTextSize(mTextSize);
paintText.getTextBounds(item.getTitle(), 0, item.getTitle()
.length(), rectText);
rectText.inset(-mTitleMargin, -mTitleMargin);
rectText.offsetTo(
markerBottomCenterCoords.x - rectText.width() / 2,
markerBottomCenterCoords.y + 5);
paintText.setTextAlign(Paint.Align.CENTER);
paintText.setTextSize(mTextSize);
paintText.setARGB(255, 255, 255, 255);
// paintText.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
paintRect.setARGB(100, 0, 0, 0);
if (mDrawCircle && mapView != null) {
int radius = metersToRadius(mRadiusInMeter, mapView);
int arcWidth = (int) ((float) radius * 0.01);
arcWidth = arcWidth == 0 ? 1 : arcWidth;
Paint paintCircle = new Paint();
paintCircle.setARGB(10, 0, 0, 255);
Paint paintArc = new Paint();
// paintArc.setColor(Color.BLUE);
paintArc.setARGB(80, 0, 0, 255);
paintArc.setStrokeWidth(arcWidth);
paintArc.setAntiAlias(true);
paintArc.setStrokeCap(Paint.Cap.ROUND);
paintArc.setStyle(Paint.Style.STROKE);
RectF rectArc = new RectF();
rectArc.set(markerBottomCenterCoords.x - radius,
markerBottomCenterCoords.y - radius,
markerBottomCenterCoords.x + radius,
markerBottomCenterCoords.y + radius);
canvas.drawCircle(markerBottomCenterCoords.x,
markerBottomCenterCoords.y, radius, paintCircle);
canvas.drawArc(rectArc, 0, 360, false, paintArc);
}
if (item.getTitle().length() > 0) {
canvas.drawRoundRect(new RectF(rectText), 2, 2, paintRect);
canvas.drawText(item.getTitle(),
rectText.left + rectText.width() / 2,
rectText.bottom - mTitleMargin, paintText);
}
}
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void removeOverlay(OverlayItem overlay) {
mOverlays.remove(overlay);
populate();
}
public void clear() {
mOverlays.clear();
populate();
}
public static int metersToRadius(float meters, MapView map) {
return (int) (map.getProjection().metersToEquatorPixels(meters));
}
public void setRadius(int radiusInMeter) {
mRadiusInMeter = radiusInMeter;
}
}