写了Android高德地图的点聚合功能
不废话:直接上代码
private ClusterOverlay mClusterOverlay;
private int clusterRadius = 100;
private void dianjuhePipeout() {
List<ClusterItem> items = new ArrayList<ClusterItem>();
if (null!=pipeoutList && !"".equals(pipeoutList) && pipeoutList.size()>0) {
for (int i = 0; i < pipeoutList.size(); i++) {
LatLng latLng = new LatLng(pipeoutList.get(i).getLat(), pipeoutList.get(i).getLng(),false);
RegionItem regionItem = new RegionItem(latLng,
"test" );
items.add(regionItem);
}
mClusterOverlay = new ClusterOverlay(mAMap, items,
dp2px(getActivity().getApplicationContext(), clusterRadius),
getActivity());
mClusterOverlay.setClusterRenderer(this);
mClusterOverlay.setOnClusterClickListener(this);
}
}
里面有一句
mClusterOverlay.setClusterRenderer(this);
这个在类里实现了
public class FragmentMain extends BaseFragment implements SensorEventListener, View.OnClickListener,ClusterRender ,ClusterClickListe
ClusterRender接口,重写了getDrawAble方法
然后看下面的代码 被注释掉的是demo原来的样式,被注释掉的下面的一行是我自己设置的样式,可以分别设置当聚合点=1时,<5,<10和>10时的样式。
private Map<Integer, Drawable> mBackDrawAbles = new HashMap<Integer, Drawable>();
@Override
public Drawable getDrawAble(int clusterNum) {
int radius = dp2px(getActivity().getApplicationContext(), 80);
if (clusterNum == 1) {
Drawable bitmapDrawable = mBackDrawAbles.get(1);
if (bitmapDrawable == null) {
bitmapDrawable =
getActivity().getApplicationContext().getResources().getDrawable(
// R.drawable.icon_openmap_mark);
R.mipmap.ic_map_paishui);
mBackDrawAbles.put(1, bitmapDrawable);
}
return bitmapDrawable;
} else if (clusterNum < 5) { //聚合数量小于5时
// Drawable bitmapDrawable = mBackDrawAbles.get(2);
Drawable bitmapDrawable = getActivity().getResources().getDrawable(R.drawable.yello_56x56);
if (bitmapDrawable == null) {
bitmapDrawable = new BitmapDrawable(null, drawCircle(radius,
Color.argb(159, 210, 154, 6)));
mBackDrawAbles.put(2, bitmapDrawable);
}
return bitmapDrawable;
} else if (clusterNum < 10) { //聚合数量小于10时
// Drawable bitmapDrawable = mBackDrawAbles.get(3);
Drawable bitmapDrawable = getActivity().getResources().getDrawable(R.drawable.red_66x65);
if (bitmapDrawable == null) {
bitmapDrawable = new BitmapDrawable(null, drawCircle(radius,
Color.argb(199, 217, 114, 0)));
mBackDrawAbles.put(3, bitmapDrawable);
}
return bitmapDrawable;
} else { //聚合数量大于10时
// Drawable bitmapDrawable = mBackDrawAbles.get(4);
Drawable bitmapDrawable = getActivity().getResources().getDrawable(R.drawable.pink_78x77);
if (bitmapDrawable == null) {
bitmapDrawable = new BitmapDrawable(null, drawCircle(radius,
Color.argb(235, 215, 66, 2)));
mBackDrawAbles.put(4, bitmapDrawable);
}
return bitmapDrawable;
}
}
当然 你也可以按顺序把drawable对象添加到mBackDrawAbles集合中,也是一样的效果,这里我就不多做说明了。
差点忘了 还有一个方法差点忘记贴了
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public int dp2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}