经过一些研究,我也没有找到比动态创建标记位图更好的选择。但我也在地图上创建了带有多边形的圆圈。请注意,这并不是真正的高性能解决方案,但就我而言,这是一个不错的选择。代码示例:
private static final int CIRCLE_POLYGON_VERTICES = 16;
private static final double EARTH_RADIUS = 6378.1d;
private List<LatLng> createCirclePolygon(LatLng center, double r) {
List<LatLng> res = new ArrayList<LatLng>(CIRCLE_POLYGON_VERTICES);
double r_latitude = MathUtils.rad2deg(r/EARTH_RADIUS);
double r_longitude = r_latitude / Math.cos(MathUtils.deg2rad(center.latitude));
for (int point = 0; point < CIRCLE_POLYGON_VERTICES + 1; point++) {
double theta = Math.PI * ((double)point / (CIRCLE_POLYGON_VERTICES / 2));
double circle_x = center.longitude + (r_longitude * Math.cos(theta));
double circle_y = center.latitude + (r_latitude * Math.sin(theta));
res.add(new LatLng(circle_y, circle_x));
}
return res;
}
private Bitmap getClusteredLabel(String cnt, Context ctx) {
Resources r = ctx.getResources();
Bitmap res = BitmapFactory.decodeResource(r, R.drawable.map_cluster_bg);
res = res.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(res);
Paint textPaint = new Paint();
textPaint.setAntiAlias(true);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(21);
c.drawText(String.valueOf(cnt), res.getWidth()/2, res.getHeight()/2 + textPaint.getTextSize() / 3, textPaint);
return res;
}
public void createClusteredOverlay(MapPinData point, GoogleMap map, Context ctx) {
if (point.getCount() > 1) {
map.addMarker(new MarkerOptions().position(point.getLatLng()).anchor(0.5f, 0.5f).icon(BitmapDescriptorFactory.fromBitmap(getClusteredLabel(String.valueOf(point.getCount()), ctx))));
map.addPolygon(new PolygonOptions()
.addAll(createCirclePolygon(point.getLatLng(), point.getRadius()))
.fillColor(Color.argb(50, 0, 0, 10))
.strokeWidth(0)
);
} else {
map.addMarker(new MarkerOptions().position(point.getLatLng()).title(point.getTitle()));
}
}
我的 MathUtils 方法:
public static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
public static double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
如果您的半径以英里为单位,则应将 EARTH_RADIUS 常量更改为英里,3963 AFAIK。