无法显示带阴影的地图标记

时间:2012-10-04 07:12:28

标签: java android android-mapview android-overlay

我正在使用自定义Overlay类在MapView窗口小部件上显示标记。我正在使用我发现的另一个例子中的相同图像,但我的叠加层却没有阴影。

这是原作:

Here's the original

..这是我的:

..and here's mine:

阴影是如何产生的?这是draw类的Overlay方法中的另一个可绘制资源或一些技巧。感谢。

-

这是我的Overlay课程:

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;
    }

}

1 个答案:

答案 0 :(得分:0)

我发现了一些显示标记阴影的代码。我需要调整偏斜因素才能使它工作,但它现在已经成功了。以下是我的自定义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);

    }

}