0

此代码使用 Google API(级别 8)。

当我更新 OverlayItem 以使用自定义可绘制对象时,Canvas 对象似乎将像素绘制在错误的位置。在这个例子中,我试图在路易斯安那州画一个圆圈。查看整个地图时,圆圈会从地图上画出。当您放大到新奥尔良时,您会看到圆圈接近适当的纬度和经度。无论在哪里绘制圆圈,热点似乎都在正确的位置。

如果在 draw 方法中调用 canvas restore 方法,则在正确的位置绘制圆。

此外,如果未使用自定义可绘制对象,则图标将绘制在正确的位置(不使用 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;
    }
}
=======================================================================
4

1 回答 1

1

CustomDrawable不应该根据地图定位自己。它应该只在其范围内绘制自己,而不是引用 theMapView或 its ProjectionItemizedOverlay负责Drawable为您的OverlayItem.

于 2011-03-01T12:50:03.407 回答