1. 程式人生 > >高德地圖自定義點聚合樣式Android

高德地圖自定義點聚合樣式Android

寫了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);
    }