javaandroidandroid-mapviewitemizedoverlay

Show the Marker at the center of the circumcircles


I am trying to draw few circles within circle and show them on the Google maps. And current location should be at the center of all the circles. Means in the middle of the circle.

Everything is working fine. But somehow my marker is not getting displayed at the center of all the circles. Marker size is 36 by 36.

I tried few hit and trials but still the same problem. Is there any way I can place the marker at the center of all the circumcircles? What is the problem with my code because of that it is not showing in the center of circle? May be the way I am drawing circle is not right?

@Override
    public void onLocationChanged(Location location) {
        if (location != null) {
        GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1E6),
            (int) (location.getLongitude() * 1E6));

        mapController.animateTo(point);
        mapController.setZoom(15);

        if (mapOverlay == null) {
            mapOverlay = new MapOverlay(this, R.drawable.mark_blue);

            List<Overlay> listOfOverlays = mapView.getOverlays();
            listOfOverlays.add(mapOverlay);
        }

        mapOverlay.setPointToDraw(point);
        mapView.invalidate();
        }
    }

Below is my MapOverlay class in which I am drawing circle within circle

class MapOverlay extends Overlay {
    private GeoPoint pointToDraw;
    int[] imageNames = new int[6];
    private Point mScreenPoints;
    private Bitmap mBitmap;
    private Paint mCirclePaint;

    public MapOverlay(GPSLocationListener gpsLocationListener, int currentUser) {
        imageNames[0] = currentUser;
        imageNames[1] = R.drawable.tenm;
        imageNames[2] = R.drawable.twentym;
        imageNames[3] = R.drawable.thirtym;
        imageNames[4] = R.drawable.fourtym;
        imageNames[5] = R.drawable.fiftym;

        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setColor(0x10000000);
        mCirclePaint.setStyle(Style.FILL_AND_STROKE);
        mBitmap = BitmapFactory.decodeResource(getResources(), imageNames[0]);
        mScreenPoints = new Point();
    }

    public void setPointToDraw(GeoPoint point) {
        pointToDraw = point;
    }

    public GeoPoint getPointToDraw() {
        return pointToDraw;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
        super.draw(canvas, mapView, shadow);
        mScreenPoints = mapView.getProjection().toPixels(pointToDraw, mScreenPoints);

        int totalCircle = 5;
        int radius = 40;
        int centerimagesize = 13;

        for (int i = 1; i <= totalCircle; i++) {
        canvas.drawCircle(mScreenPoints.x, mScreenPoints.y, i * radius, mCirclePaint);
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), imageNames[i]),
            ((mScreenPoints.x) + (i * radius)), (mScreenPoints.y), null);
        }

        canvas.drawBitmap(mBitmap, (mScreenPoints.x - (centerimagesize / 2)),
            (mScreenPoints.y - (centerimagesize / 2)), null);
        super.draw(canvas, mapView, shadow);

        return true;
        }
    }

Below is the screenshot from my communicator as well in which you can clearly see, it is not coming in the center of circle-

enter image description here

Can anyone provide some thoughts on this?


Solution

  • It looks to me like the top-left corner of the marker graphic is in the centre of the circle. So you need to adjust the marker position by the displacement from the top-left corner to the actual point of the marker. I guess that would be 18 horizontally and 36 vertically.