开发者

Problem with drawing location using a custom Drawable in an Android OverlayItem

This code is using the Google APIs (level 8).

When I update the OverlayItem to use a custom drawable the Canvas object seems to draw the pixels in the wrong location. In this example I'm trying to draw a circle in Louisiana. When viewing the entire map the circle is drawn off the map. As you zoom into New Orleans you'll see the circle approach the appropriate latitude and longitude. The hot spot seems to be in the correct location, no matter where the circle is being drawn.

If, in the draw method, the canvas restore method is called the circle draws in the correct location.

Also If the custom drawable is not used, the icon is drawn in the correct location (without using Canvas "restore").

Below is the code showing this behavior. I tried adding "setBounds" and "boundCenterBottom", since other people seemed to indicate that resolved their "wrong location" problems. Though to be honest I'm not sure why those calls are needed.

=======================================================================
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());
        开发者_JAVA百科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;
    }
}
=======================================================================


Your CustomDrawable shouldn't be positioning itself with respect to the map. It should just draw itself within its bounds, and not reference the MapView or its Projection. ItemizedOverlay takes care of positioning the Drawable for your OverlayItem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜