Draw five transparent circumcircles on the Google Map v2

你说的曾经没有我的故事 提交于 2019-12-04 01:51:05

问题


I have recently started working with Google Map v2 and figured it out lot of things have been changed. Previously I was using Google Map v1 so I was using the concept of MapView.

I am trying to create five transparent circumcircles with the center as my current location. Below is the code, I was using for Google Map v1 to draw the circle and it was working fine for me. Now I am trying to draw the same circle that I have drawn in my below code on the Google Map v2. I cannot use MapView here as I am using GoogleMap object here. Can anyone help me with this to draw the circle on the Google Map v2 with center as my current location

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

            // Create a LatLng object for the current location
            LatLng latLng = new LatLng(location.getLatitude(),  location.getLongitude());

            // Show the location on the Google Map
            googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

            // Zoom in the Google Map
            googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

        //   Below is the code for Google Map v1 to draw the circle on the map  

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

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

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

A simple class that extends Overlay and draw the circle on the Google Map v1

class MapOverlay extends Overlay {
    private GeoPoint pointToDraw;
    int[] imageNames = new int[6];

    // This is the cached Point on the screen that will get refilled on
    // every draw
    private Point mScreenPoints;

    // This is the cached decoded bitmap that will be drawn each time
    private Bitmap mBitmap;

    // Cached Paint
    private Paint mCirclePaint;

    public MapOverlay(ProximityLocationListener 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;

        // This only needs to be made here, once. It never needs to change.
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setColor(0x10000000);
        mCirclePaint.setStyle(Style.FILL_AND_STROKE);

        // We only need to load this image once and then just keep drawing
        // it when dirtyed.
        mBitmap = BitmapFactory.decodeResource(getResources(),
                imageNames[0]);

        // This Point object will be changed every call to toPixels(), but
        // the instance can be recycled
        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 + 18, mScreenPoints.y + 36, 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;
    }
}

I just need to draw the same circle that I have drawn in my above code on the Google Map v2. Is there any way, I can use the above code with GoogleMap object so that I can draw circles on the Google Map v2?

Thanks for the help.

Updated Code:-

I need to draw five circle-circle on the Google Maps v2 by taking the current location as the center of a circle. Meaning that each of the five circles have the same center but with different radii: the first circle will have radius of 10m, second circle radius of 20m, third circle radius of 30m, fourth circle radius of 40m, and fifth circle radius of 50m. I am using Google Maps v2.

And I need to show a marker on the center of the circle as well.

I am trying something like this to draw the circle on the Google Map v2 but it draws only one circle and not the five circum-circle

CircleOptions circleOptions = new CircleOptions()
                  .center(latLng)   //set center
                  .radius(500)   //set radius in meters
                  .fillColor(Color.TRANSPARENT)  //default
                  .strokeColor(0x10000000)
                  .strokeWidth(5);

                  myCircle = googleMap.addCircle(circleOptions);

I need to draw circum-circle exactly like this-

Can anybody help me with this? I am having problem in making this circle in Google Map v2. Any help will be appreciated.


回答1:


Just add a circle to the map using googleMap.addCircle(...)

see documentation

Update:

I did exactly as I said to do and I had no problem doing what you want

here is the code, super simple super basic....

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = super.onCreateView(inflater, container, savedInstanceState);
    GoogleMap map = getMap();

    int radius = 500;

    for(int i=0; i<5; i++){
        map.addCircle(new CircleOptions().center(new LatLng(0,0)).radius(radius).fillColor(0x30000000).strokeWidth(3));
        radius += 500;
    }
    return view;
}


来源:https://stackoverflow.com/questions/16679408/draw-five-transparent-circumcircles-on-the-google-map-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!