在Android中的地图视图上绘制一定半径的圆

Pra*_*u M 12 geometry android google-maps

我想在地图视图上画一个圆圈.我希望用户输入半径,对于该半径,我必须在地图上显示圆圈.之后,我必须在该圈子的某些位置显示标记.

我知道如何在地图视图上显示标记.

如何在地图视图上绘制圆圈并在该圆形边界上显示标记.

war*_*rbi 30

只是为了让它更新......他们已经在Google Maps API v2上轻松搞定了.

    mMap.addCircle(new CircleOptions()
        .center(center)
        .radius(radius)
        .strokeWidth(0f)
        .fillColor(0x550000FF));
Run Code Online (Sandbox Code Playgroud)

半径以米为单位.

至于边界上的标记,这应该相对容易 - 只需按照Google地图示例代码中的'圈子'演示进行操作:https://developers.google.com/maps/documentation/android/intro#sample_code


Gri*_*a S 10

如果有人正在使用Google Maps API v2寻找答案,请参阅以下内容.这更像是一种地理方法.

public class MapDrawer {

private GoogleMap map;
private static int EARTH_RADIUS = 6371000;

public MapDrawer(GoogleMap map) {
    this.map = map;
}

private LatLng getPoint(LatLng center, int radius, double angle) {
    // Get the coordinates of a circle point at the given angle
    double east = radius * Math.cos(angle);
    double north = radius * Math.sin(angle);

    double cLat = center.latitude;
    double cLng = center.longitude;
    double latRadius = EARTH_RADIUS * Math.cos(cLat / 180 * Math.PI);

    double newLat = cLat + (north / EARTH_RADIUS / Math.PI * 180);
    double newLng = cLng + (east / latRadius / Math.PI * 180);

    return new LatLng(newLat, newLng);
}

public Polygon drawCircle(LatLng center, int radius) {
    // Clear the map to remove the previous circle
    map.clear();
    // Generate the points
    List<LatLng> points = new ArrayList<LatLng>();
    int totalPonts = 30; // number of corners of the pseudo-circle
    for (int i = 0; i < totalPonts; i++) {
        points.add(getPoint(center, radius, i*2*Math.PI/totalPonts));
    }
    // Create and return the polygon
    return map.addPolygon(new PolygonOptions().addAll(points).strokeWidth(2).strokeColor(0x700a420b));
}
Run Code Online (Sandbox Code Playgroud)

}

这样做的好处是,您无需在缩放或平移地图后重绘任何内容 - 圆圈会调整大小并相应地移动.缺点是,如果你想要一个北极或南极的圆圈,它就不会起作用了 - 它们都会变成bezerk,但是,希望99%的情况不是这样的情况:)


Eli*_*ine 8

在执行中ItemizedOverlay,执行类似方法drawCircleonDraw方法

protected void drawCircle(Canvas canvas, Point curScreenCoords) {
    curScreenCoords = toScreenPoint(curScreenCoords);
    int CIRCLE_RADIUS = 50;
    // Draw inner info window
    canvas.drawCircle((float) curScreenCoords.x, (float) curScreenCoords.y, CIRCLE_RADIUS, getInnerPaint());
    // if needed, draw a border for info window
    canvas.drawCircle(curScreenCoords.x, curScreenCoordsy, CIRCLE_RADIUS, getBorderPaint());
}

private Paint innerPaint, borderPaint;

public Paint getInnerPaint() {
    if (innerPaint == null) {
        innerPaint = new Paint();
        innerPaint.setARGB(225, 68, 89, 82); // gray
        innerPaint.setAntiAlias(true);
    }
    return innerPaint;
}

public Paint getBorderPaint() {
    if (borderPaint == null) {
        borderPaint = new Paint();
        borderPaint.setARGB(255, 68, 89, 82);
        borderPaint.setAntiAlias(true);
        borderPaint.setStyle(Style.STROKE);
        borderPaint.setStrokeWidth(2);
    }
    return borderPaint;
}

@Override
protected void onDraw(Canvas canvas) {
    Point p = new Point();
    for(OverlayItem item : items) {
        drawCircle(canvas, getProjection().toPixels(item.getPoint(), p));
    }
}
Run Code Online (Sandbox Code Playgroud)