使用Android OverlayItem中的自定义Drawable绘制位置的问题

时间:2011-02-15 11:44:29

标签: android location marker overlayitem

此代码正在使用 Google API(第8级)。

当我更新OverlayItem以使用Canvas自定义drawable时 对象似乎在错误的位置绘制像素。在这 例如,我正试图在路易斯安那州画一个圆圈。查看时 整个地图圆圈从地图上绘制出来。当你放大到新的 奥尔良,你会看到圆圈接近适当的纬度和 经度。热点似乎在正确的位置,没有 圆圈被绘制的地方。

如果在draw方法中,canvas恢复方法被称为circle 画在正确的位置。

此外,如果未使用自定义drawable,则会在中绘制图标 正确的位置(不使用Canvas“恢复”)。

以下是显示此行为的代码。我尝试添加“setBounds” 和“boundCenterBottom”,因为其他人似乎表明了这一点 解决了他们的“错位”问题。虽然 说实话,我不确定为什么需要这些电话。

=======================================================================
public class MapsActivity extends MapActivity
{
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MapView mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);

        // Itemized Overlay
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable defaultIcon =
this.getResources().getDrawable(R.drawable.icon);
        MyItemizedOverlay itemizedoverlay = new
MyItemizedOverlay(defaultIcon, this);

        // Overlay Item
        GeoPoint pt = new GeoPoint(30000000, -90000000);
        OverlayItem item = new OverlayItem(pt,"New Orleans",
"Louisiana");

        // Custom Drawable
        CustomDrawable customDrawable = new CustomDrawable(pt,
mapView);

        boolean showProblem = true;
        if (showProblem)
        {
            item.setMarker(customDrawable);
        }
        else
        {
            item.setMarker(defaultIcon);
        }

        // Add item we want to overlay
        itemizedoverlay.addOverlay(item);

        // Add overlay
        mapOverlays.add(itemizedoverlay);
    }

    protected boolean isRouteDisplayed()
    {
        return false;
    }
}
=======================================================================
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
    private ArrayList<OverlayItem> mOverlays = new
ArrayList<OverlayItem>();

    private Context mContext;

    public MyItemizedOverlay(Drawable defaultMarker, Context context)
    {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }

    public void addOverlay(OverlayItem item)
    {
        mOverlays.add(item);
        populate();
    }

    public void removeOverlay(OverlayItem item)
    {
        mOverlays.remove(item);
    }

    public void removeOverlay(int item)
    {
        mOverlays.remove(item);
    }

    protected OverlayItem createItem(int i)
    {
        OverlayItem item = mOverlays.get(i);
        Drawable drawable = item.getMarker(0);

        if (drawable != null)
        {
            int w = drawable.getIntrinsicWidth();
            int h = drawable.getIntrinsicHeight();

            drawable.setBounds(0, 0, w, h);

            item.setMarker(boundCenterBottom(drawable));
        }

        return item;
    }

    public void draw(android.graphics.Canvas canvas, MapView mapView,
            boolean shadow)
    {
        if (shadow)
            return;

        super.draw(canvas, mapView, shadow);
    }

    public int size()
    {
        return mOverlays.size();
    }

    protected boolean onTap(int index)
    {
        OverlayItem item = mOverlays.get(index);
        AlertDialog.Builder dialog = new
AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();

        return true;
    }
}

=======================================================================
public class CustomDrawable extends ShapeDrawable
{
    private int radius = 10;
    private GeoPoint point = null;
    private MapView mapView = null;

    public CustomDrawable(GeoPoint point, MapView mapView)
    {
        this.point  = point;
        this.mapView  = mapView;
    }

    public void draw(Canvas canvas)
    {
        // TODO This (somewhat) fixes projection problem?
        //canvas.restore();

        Projection projection = mapView.getProjection();

        Point pt = projection.toPixels(point, null);

        canvas.drawCircle(pt.x, pt.y, radius,
getPaint());
    }

    public int getIntrinsicHeight()
    {
        return 2 * radius;
    }

    public int getIntrinsicWidth()
    {
        return 2 * radius;
    }
}
=======================================================================

1 个答案:

答案 0 :(得分:1)

您的CustomDrawable不应该相对于地图进行自我定位。它应该只是在其范围内绘制,而不是引用MapView或其ProjectionItemizedOverlay负责为Drawable定位OverlayItem