Search code examples
javaandroidandroid-mapviewandroid-overlay

Unable to display a map marker with a shadow


I'm using a custom Overlay class to display a marker on a MapView widget. I'm using the same image from another example that I found but my overlay is missing the shadow.

Here's the original:

Here's the original

..and here's mine:

..and here's mine:

How is that shadow created? Is that another drawable resource or some tricks in the draw method of the Overlay class. Thanks.

--

Here's my Overlay class:

public class Mappin extends com.google.android.maps.Overlay {

    private final GeoPoint geoPoint;
    private final Context ctxContext;

    public Mappin(Context ctxContext, GeoPoint geoPoint) {
        super();

        this.geoPoint = geoPoint;
        this.ctxContext = ctxContext;
    }

    public boolean draw(Canvas canCanvas, MapView mvwMap, boolean booShadow, long lngWhen) {
        super.draw(canCanvas, mvwMap, booShadow);

        Point screenPts = new Point();
        mvwMap.getProjection().toPixels(this.geoPoint, screenPts);

        Bitmap bmp = BitmapFactory.decodeResource(this.ctxContext.getResources(), R.drawable.ic_location_marker);
        canCanvas.drawBitmap(bmp, screenPts.x - bmp.getWidth() / 2, screenPts.y - bmp.getHeight(), null);

        return true;
    }

}

Solution

  • I found some code one that displayed the marker shadow. I needed to tweak the skewing factor to get it to work but it did the trick for now. Here's the entire code of my custom Overlay:

    public class Mappin extends Overlay {
    
        protected Drawable drwMarker;
        protected GeoPoint gptCoordinates;
    
        public Mappin(Drawable drwMarker, GeoPoint gptCoordinates) {
    
            this.drwMarker = drwMarker;
            this.gptCoordinates = gptCoordinates;
    
        }
    
        @Override
        public void draw(Canvas canCanvas, MapView mapView, boolean booShadow) {
    
            super.draw(canCanvas, mapView, booShadow);
    
            Projection prjProjection = mapView.getProjection();
    
            Integer x;
            Integer y;
    
            if (!booShadow) {
    
                x = prjProjection.toPixels(gptCoordinates, null).x - (drwMarker.getIntrinsicWidth() / 2);
                y = prjProjection.toPixels(gptCoordinates, null).y - (drwMarker.getIntrinsicHeight());
    
            } else {
    
                Integer intSign = (SHADOW_X_SKEW > 0 ? 1 : -1);
                Float fltScaler = 1.1F - Math.abs(SHADOW_X_SKEW);
                x = (int) (prjProjection.toPixels(gptCoordinates, null).x - (intSign * (drwMarker.getIntrinsicWidth() * fltScaler)));
                y = (int) (prjProjection.toPixels(gptCoordinates, null).y - (drwMarker.getIntrinsicHeight() * SHADOW_Y_SCALE));
    
            }
    
            drawAt(canCanvas, drwMarker, x, y, booShadow);
    
        }
    
    }