在Google地图V2 for Android中替换ItemizedOverlay?

Mar*_*ers 6 maps android

在Google Maps for Android的V1中,我使用ItemizedOverlay类实现了一种聚类形式.每个群集都有一个中心点(纬度/经度),半径(以英里为单位)和群集中项目数的计数.我使用了ItemizedOverlay.draw(Canvas,MapView,shadow)来使用Canvas对象方法绘制集群,例如drawCircle()和drawText().每个簇由包含计数的实心圆(根据计数所需的空间确定)和显示半径的圆组成.

在阅读V2上的文档并使用演示应用程序后,我看到没有相当于ItemizedOverlay,也没有明显的选择.我怀疑这样做的唯一方法是自己维护一个集群列表,然后继承MapView并提供其onDraw()方法的实现.

我是否遗漏了V2 API中哪些东西比继承MapView更好?

谢谢,

标记

use*_*058 4

经过一些研究,我也没有找到比动态为标记创建位图更好的选择。但我也在地图上创建带有多边形的圆圈。请注意,这并不是真正的高性能解决方案,但对于我的情况来说,这是一个不错的选择。代码示例:

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()));
    }
}
Run Code Online (Sandbox Code Playgroud)

我的 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);
}   
Run Code Online (Sandbox Code Playgroud)

如果半径以英里为单位,则应将 EARTH_RADIUS 常量更改为英里,据我所知,3963。