如何使用Google Maps API v2在标记周围绘制圆形

16
我正在为我的Android应用程序使用新的API(Google Map API V2),我已经创建了地图并向其添加了标记,现在我的任务是手动在任何标记周围创建圆圈,并且我希望为用户提供一个功能,使他可以相应地增加该圆的半径。为此,我提供了一条滑动条,当用户增加该条时,圆的半径将增加,反之亦然。
如果有人知道如何使用Google Map API V2实现此操作,请帮忙,
谢谢

请参考以下链接:https://dev59.com/E2Yr5IYBdhLWcg3wDGHP - zmeda
4个回答

16

我也一直在处理这个问题,最终找到了以下解决方案。尽管不完美,但我必须使用非常大的画布才能避免圆形边缘变得模糊。

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));
}

-- 编辑 -- Google 更新了 API。现在您可以轻松地向地图添加一个圆: https://developers.google.com/maps/documentation/android/shapes?hl=nl#circles


谢谢您宝贵的代码,它帮助了我很多...但是如何设置圆形中心?我需要将标记本身作为中心。 - Akshatha S R
1
你修改后的答案真的帮助了我很多。谢谢。 - MetaSnarf
@spes:地图缩放时,地面叠加层的大小会发生变化。有没有办法保持它的恒定? - user2234

11

这个更好:

    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);

5
也许我可以帮助您:
 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));

从这里开始:

此处


1
使用此方法,您可以选择任何标记并创建特定标记的圆形对象。通过将标记对象和半径值传递给createCircle()方法,您可以动态更改圆的半径。
 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));
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接