Salman Khan
Salman Khan

Reputation: 2832

How to draw circle around a pin using Google MAps API v2

I am using the new API(Google Map API V2) for my android application, i have done creating the map and adding markers to it, now my task is to manually create a circle around any of the marker and also i want to provide a functionality to the user that he can increase the radius of that circle accordingly, for this i have given a bar, when user increases that bar the radius of circle will increase and vice versa.

If anybody knows how to do this using Google Map API V2 then please help,

thanks

Upvotes: 16

Views: 33855

Answers (4)

jeevashankar
jeevashankar

Reputation: 1508

Using this method you can select any marker & it will create the circle object for particular marker. you can dynamically change radius of the circle by passing the marker object & radius value to the createCircle() method.

 private GoogleMap mMap;
    /*Create circle objects*/
    Circle currentCircle;
     /**
     * create circle when user want to set region
     * @param currentMarker this is user selected marker
     * @param radius pass radius value to circle object
     */
    private void createCircle(Marker currentMarker ,Double radius){


          //check circle is exist or not 
               //if exist remove
               if(currentCircle!=null){
                 currentCircle.remove();
                 }
        currentCircle=mMap.addCircle(new CircleOptions().center(currentMarker.getPosition()).radius(radius)
            .strokeColor(Color.parseColor("#FF007A93"))
            .fillColor(Color.parseColor("#40007A93"))
            .strokeWidth(2));
        float zoomLevel = getZoomLevel(radius);
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentMarker.getPosition(), zoomLevel));
    }

Upvotes: 1

rose rosa
rose rosa

Reputation: 51

Perhaps help you :

 GoogleMap map;
 // ... get a map.
 // Add a circle in Sydney
 Circle circle = map.addCircle(new CircleOptions()
     .center(new LatLng(-33.87365, 151.20689))
     .radius(10000)
     .strokeColor(Color.RED)
     .fillColor(Color.BLUE));

From here :

HERE

Upvotes: 5

Parthi
Parthi

Reputation: 658

This is better:

    double radiusInMeters = 100.0;
     //red outline
    int strokeColor = 0xffff0000;
    //opaque red fill
    int shadeColor = 0x44ff0000; 


    CircleOptions circleOptions = new CircleOptions().center(position).radius(radiusInMeters).fillColor(shadeColor).strokeColor(strokeColor).strokeWidth(2);
    mCircle = map.addCircle(circleOptions);

    MarkerOptions markerOptions = new MarkerOptions().position(position);
    mMarker = map.addMarker(markerOptions);

Upvotes: 11

spes
spes

Reputation: 484

I have been working on this too and I found the following solution. It's still not perfect, because I had to make a very large Canvas to prevent the edge of the circle from becoming blurry.

private void addCircleToMap() {

    // circle settings  
    int radiusM = // your radius in meters
    double latitude = // your center latitude
    double longitude = // your center longitude
    LatLng latLng = new LatLng(latitude,longitude);

    // draw circle
    int d = 500; // diameter 
    Bitmap bm = Bitmap.createBitmap(d, d, Config.ARGB_8888);
    Canvas c = new Canvas(bm);
    Paint p = new Paint();
    p.setColor(getResources().getColor(R.color.green));
    c.drawCircle(d/2, d/2, d/2, p);

    // generate BitmapDescriptor from circle Bitmap
    BitmapDescriptor bmD = BitmapDescriptorFactory.fromBitmap(bm);

// mapView is the GoogleMap
    mapView.addGroundOverlay(new GroundOverlayOptions().
            image(bmD).
            position(latLng,radiusM*2,radiusM*2).
            transparency(0.4f));
}

-- EDIT -- Google updated the API. You can now easily add a circle to your map: https://developers.google.com/maps/documentation/android/shapes?hl=nl#circles

Upvotes: 16

Related Questions