pawel
pawel

Reputation: 6166

How to draw a circle above the mapview in Android?

I want to draw a circle on the layer ABOVE my Google Maps MapView in Android. I mean - there is a red circle in the center of the mapview, but wherever I move my map - this cirle should ALWAYS be in the center of my view - something like one layer above mapview.

Can you tell me how to do it?

Thanks!

Upvotes: 0

Views: 797

Answers (3)

NickT
NickT

Reputation: 23873

Why not use a simple overlay? Something like:

public class MapDemo extends MapActivity  { 
    MapView mMapView;
    CircleOverlay mCOverlay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GeoPoint mapCentrePoint = new GeoPoint(51500000, 0);
        mMapView = (MapView) findViewById(R.id.mapview);
        MapController mapCtrlr = mMapView.getController();
        mapCtrlr.setZoom(8);
        mapCtrlr.setCenter(mapCentrePoint);
        List<Overlay> listOfOverlays = mMapView.getOverlays();
        mCOverlay = new CircleOverlay(50.0f);
        listOfOverlays.add(mCOverlay);
    }

    @Override
    protected boolean isRouteDisplayed() {return false;}

    public class CircleOverlay extends com.google.android.maps.Overlay {
        Paint mPaint;
        float mRadius;
        public CircleOverlay(float radius) {
            super();
            mRadius = radius;
            mPaint = new Paint();
            mPaint.setColor(Color.RED);
            mPaint.setStyle(Style.STROKE);
            mPaint.setStrokeWidth(2);
        }
        @Override
        public boolean draw(Canvas canvas, MapView mv, boolean shadow,
                long when) {
            canvas.drawCircle(mv.getWidth()/2, mv.getHeight()/2, 
                    mRadius, mPaint);
            return false;
        }
    }
}

.

Upvotes: 0

VinceFR
VinceFR

Reputation: 2571

Use a FrameLayout, its first child will be the MapView, and its second child will be your circle(with Gravity = CENTER).

Upvotes: 2

Related Questions