Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Osmdroid. onItemSingleTapUp not works for custom Drawable

I have created custom drawable marker which uses canvas to draw bounds. Everything works great excepts one thing: onItemSingleTapUp not called when any marker on the screen taped. Here is overlay creation code:

ItemizedIconOverlay<OverlayItem> groupsOverlay = new ItemizedIconOverlay<OverlayItem>(
    new ArrayList<OverlayItem>(),
    new OnItemGestureListener<OverlayItem>() {

        @Override
        public boolean onItemLongPress(int arg0, OverlayItem arg1) {                                
            return false;
        }

        @Override
        public boolean onItemSingleTapUp(int arg0, OverlayItem arg1) {

            if(arg1 == null){
                return false;
            }

            if(m_prevView != null){
                m_mapView.removeView(m_prevView);
                m_prevView = null;
            }

            View popUp = getLayoutInflater().inflate(R.layout.map_popup, m_mapView, false);
            TextView tv = (TextView)popUp.findViewById(R.id.popupTextView);
            tv.setText(arg1.getTitle());

            MapView.LayoutParams mapParams = new MapView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT,
        arg1.getPoint(),
        MapView.LayoutParams.BOTTOM_CENTER, 0, 0);
            m_mapView.addView(popUp, mapParams);
            m_prevView = popUp;

            return true;
        }
    }, new DefaultResourceProxyImpl(getApplicationContext()));

This is custom drawable marker:

package com.testapp.data; 
import android.graphics.Canvas; 
import android.graphics.ColorFilter; 
import android.graphics.Paint; 
import android.graphics.Paint.Align; 
import android.graphics.Paint.Style; 
import android.graphics.PixelFormat; 
import android.graphics.Rect; 
import android.graphics.RectF; 
import android.graphics.drawable.Drawable; 
public class GroupMarkerDrawable extends Drawable { 
        private final static int DELTA_BOX = 4; 
        private final Paint m_paint; 
        private String m_text; 
        private double m_pxRadius; 
        public GroupMarkerDrawable(double pxRadius, String text) { 
                m_text = text; 
                m_paint = new Paint(); 
                m_pxRadius = pxRadius; 
                m_paint.setAntiAlias(true); 
        } 
        @Override 
    public void draw(Canvas c) { 
                // Set the correct values in the Paint 
        m_paint.setARGB(190, 0, 0, 0); 
        m_paint.setStrokeWidth(2); 
        m_paint.setStyle(Style.STROKE); 
        m_paint.setTextAlign(Align.CENTER); 
        Rect bounds = new Rect(); 
        m_paint.getTextBounds(m_text, 0, m_text.length(), bounds); 
        int centerX = getBounds().centerX(); 
        int centerY = getBounds().centerY(); 
        int w2 = bounds.width() / 2; 
        int h2 = bounds.height() / 2; 
        Rect rect = new Rect(centerX - w2 - DELTA_BOX, centerY - h2 - 
DELTA_BOX, centerX + w2 + DELTA_BOX, centerY + h2 + DELTA_BOX); 
        // Draw it 
        c.drawCircle(centerX, centerY, (float) m_pxRadius, m_paint); 
        m_paint.setStyle(Style.FILL); 
        m_paint.setARGB(190, 0, 128, 0); 
        c.drawRect(rect, m_paint); 
        m_paint.setStyle(Style.STROKE); 
        m_paint.setARGB(190, 0, 0, 0); 
        c.drawRect(rect, m_paint); 
        c.drawText(m_text, centerX, centerY + h2, m_paint); 
    } 
        @Override 
        public int getOpacity() { 
                return PixelFormat.OPAQUE; 
        } 
        @Override 
        public void setAlpha(int arg0) { 
        } 
        @Override 
        public void setColorFilter(ColorFilter arg0) { 
        } 
} 

Same code using static drawable from resources, instead of GroupMarkerDrawable, works.

like image 320
ydanila Avatar asked Nov 04 '22 23:11

ydanila


1 Answers

I found how to solve this. Just need to return proper dimensions for Drawable. This requires overriding of two methods in GroupMarkerDrawable. FOr example like this:

@Override
public int getIntrinsicHeight() {
    return m_pxRadius * 2;
};

@Override
public int getIntrinsicWidth() {
    return m_pxRadius * 2;
};
like image 87
ydanila Avatar answered Nov 15 '22 00:11

ydanila