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

后端 未结 5 1175
刺人心
刺人心 2021-01-31 20:31

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 an

5条回答
  •  暖寄归人
    2021-01-31 21:22

    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

提交回复
热议问题