开发者

Android Google Map Drawing Text on Multiple Overlays with Custom Marker

开发者 https://www.devze.com 2023-03-17 22:07 出处:网络
I need to draw text on a custom marker which are quite a lot in number. But the problem is that when I draw the text in the Overridden on draw, all the overlay items text appears to be in one layer ab

I need to draw text on a custom marker which are quite a lot in number. But the problem is that when I draw the text in the Overridden on draw, all the overlay items text appears to be in one layer above the markers and does not seem synched when the map is zoomed in or zoomed out. Previously I was calling the populate when each item was added and this was working fine but it was too slow. Any help?

In my custom class that extends from ItemizedOverlay, the constructor is as follows, where I set the custom marker:

    public HotelMapFilterOverlay(Drawable defaultMarker, final Context con) {
    super(boundCenterBottom(defaultMarker));
    this.marker = defaultMarker;
    this.con = con;
}

and the overridden draw method is as follows:

    @Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
     super.draw(canvas, mapView, shadow);
     Bitmap bmp = ((BitmapDrawable) marker).getBitmap();

     Paint textPaint = new Paint();
     t开发者_开发百科extPaint.setColor(Color.WHITE);
     textPaint.setTextSize(16f);
     textPaint.setTypeface(Typeface.create(Typeface.SANS_SERIF, 1));
     textPaint.setStyle(Paint.Style.FILL);

     Point screenPts = new Point();

     float bmpWidth = bmp.getWidth();
     float bmpHeight = bmp.getHeight();

     float left;
     float top;

     int total = mOverlayItems.size();
     for (int index = 0; index < total; index++) {

    gp = mOverlayItems.get(index).getPoint();
    mapView.getProjection().toPixels(gp, screenPts);

    left = screenPts.x - (bmpWidth / 2);
    top = screenPts.y - bmpHeight;

    // draw text
    this.title = mOverlayItems.get(index).getTitle();
    canvas.drawText(this.title, left + 8, top + 30,textPaint);          
    }
}

and I calling the populate only once to make this efficient like

public void callPopulate(){
   populate();
}


To get your marker text to appear separately over each marker without all of it layering on top of the markers, you need to draw the markers yourself and remove super.draw(canvas, mapView, shadow).

To draw the markers you have most of the code already! Just create a new Paint markerPaint = new Paint() object and add the following in your for-loop before you draw your text:

canvas.drawBitmap(bmp, screenPts.x-(bmp.getWidth()/2), screenPts.y-(bmp.getHeight()), markerPaint);

This way the marker is drawn then its text is drawn on top.

With respect to 'populate' you should call this just after you add all the items to your ItemizedOverlay.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号