我想在地图视图上画一个圆圈。我希望用户输入半径,对于该半径,我必须在地图上显示圆圈。之后,我必须在该圈子的某些位置显示标记。
我知道如何在地图视图上显示标记。
如何在地图视图上绘制圆圈并在该圆圈边界上显示标记。
答案 0 :(得分:29)
只是为了让它更新......他们已经在Google Maps API v2上轻松搞定了。
mMap.addCircle(new CircleOptions()
.center(center)
.radius(radius)
.strokeWidth(0f)
.fillColor(0x550000FF));
半径以米为单位。
至于边界上的标记,这应该相对容易 - 只需按照Google地图示例代码中的'圈子'演示进行操作:https://developers.google.com/maps/documentation/android/intro#sample_code
答案 1 :(得分: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));
}
}
关于这一点的好处是,您无需在缩放或平移地图后重绘任何内容 - 圆圈会调整大小并相应地移动。 不利的一面是,如果你想要一个北极或南极的圆圈,它就不会起作用了 - 它们都会变成bezerk,但是,希望99%的时间情况并非如此:)
答案 2 :(得分:8)
在ItemizedOverlay
的实施中,执行drawCircle
方法
onDraw
之类的操作
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));
}
}
答案 3 :(得分:4)
这不是完美的,但是这里有一些代码,我把它放在一张地图上。您可以轻松地对其进行扩展以设置圆的颜色等。我见过的大多数其他代码示例都没有考虑使用缩放级别缩放圆形大小,这是创建圆时的常见要求。圆半径以米为单位。
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
public class CircleOverlay extends Overlay {
Context context;
double mLat;
double mLon;
float mRadius;
public CircleOverlay(Context _context, double _lat, double _lon, float radius ) {
context = _context;
mLat = _lat;
mLon = _lon;
mRadius = radius;
}
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
Projection projection = mapView.getProjection();
Point pt = new Point();
GeoPoint geo = new GeoPoint((int) (mLat *1e6), (int)(mLon * 1e6));
projection.toPixels(geo ,pt);
float circleRadius = projection.metersToEquatorPixels(mRadius);
Paint innerCirclePaint;
innerCirclePaint = new Paint();
innerCirclePaint.setColor(Color.BLUE);
innerCirclePaint.setAlpha(25);
innerCirclePaint.setAntiAlias(true);
innerCirclePaint.setStyle(Paint.Style.FILL);
canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, innerCirclePaint);
}
}
答案 4 :(得分:0)
如果您将以下代码放入叠加层的绘图方法中,它将在mapView的中心绘制一个20 px的圆半径
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
....
....
Paint lp4;
lp4 = new Paint();
lp4.setColor(Color.RED);
lp4.setAntiAlias(true);
lp4.setStyle(Style.STROKE);
canvas.drawCircle(mapView.getWidth()/2, mapView.getHeight()/2, 20, lp4);
....
....
mapView.invalidate();
}
您应该能够根据自己的需要进行调整
答案 5 :(得分:0)
对于Kotlin,可以在地图中心绘制圆圈,您可以使用
mMap.setOnCameraIdleListener {
val midLatLng: LatLng = mMap.cameraPosition.target
mMap.addCircle(CircleOptions()
.center(midLatLng)
.radius(radiusInMeters)
.strokeWidth(1f)
.fillColor(0x880000FF))
}
mMap
是GoogleMap