安卓地图兴趣点 聚合加载

加载地图 我们一般根据业务需求会添加一些 兴趣点,有时我们需要加载很多,如果按照普通的加载的话,会显得界面很卡顿,所以从网上参考一下资料,这里是聚合形式的方式来实现
代码:如下

    /** * 获取视野内的marker 根据聚合算法合成自定义的marker 显示视野内的marker */
    private void resetMarks() {
        System.out.println("markerOptionsList.size():"
                + markerOptionsList.size());
        Projection projection = aMap.getProjection();
        Point p = null;
        markerOptionsListInView.clear();
        // 获取在当前视野内的marker;提高效率
        for (MarkerOptions mp : markerOptionsList) {
            p = projection.toScreenLocation(mp.getPosition());
            if (p.x < 0 || p.y < 0 || p.x > width || p.y > height) {
                // 不添加到计算的列表中
            } else {
                markerOptionsListInView.add(mp);
            }
        }
        // 自定义的聚合类MyMarkerCluster
        ArrayList<MyMarkerCluster> clustersMarker = new ArrayList<MyMarkerCluster>();
        for (MarkerOptions mp : markerOptionsListInView) {
            if (clustersMarker.size() == 0) {
                clustersMarker.add(new MyMarkerCluster(MainActivity.this,
                        mp, projection, 60));// 100根据自己需求调整
            } else {
                boolean isIn = false;
                for (MyMarkerCluster cluster : clustersMarker) {
                    if (cluster.getBounds().contains(mp.getPosition())) {
                        cluster.addMarker(mp);
                        isIn = true;
                        break;
                    }
                }
                if (!isIn) {
                    clustersMarker.add(new MyMarkerCluster(
                            MainActivity.this, mp, projection, 60));
                }
            }
        }
        // 设置聚合点的位置和icon
        for (MyMarkerCluster mmc : clustersMarker) {
            mmc.setpositionAndIcon();
        }
        aMap.clear();
        // 重新添加
        for (MyMarkerCluster cluster : clustersMarker) {
            aMap.addMarker(cluster.getOptions());
        }
    }
    /** * 高德地图聚合 * */

public class MyMarkerCluster {
    private Activity activity;
    private MarkerOptions options;
    private ArrayList<MarkerOptions> includeMarkers;
    private LatLngBounds bounds;// 创建区域

    /** * * @param activity * @param firstMarkers * @param projection * @param gridSize 区域大小参数 */
    public MyMarkerCluster(Activity activity, MarkerOptions firstMarkers,
            Projection projection, int gridSize) {
        // TODO Auto-generated constructor stub
        // this.options = firstMarkers;
        options = new MarkerOptions();
        this.activity = activity;
        Point point = projection.toScreenLocation(firstMarkers.getPosition());
        Point southwestPoint = new Point(point.x - gridSize, point.y + gridSize);
        Point northeastPoint = new Point(point.x + gridSize, point.y - gridSize);
        bounds = new LatLngBounds(
                projection.fromScreenLocation(southwestPoint),
                projection.fromScreenLocation(northeastPoint));
        options.anchor(0.5f, 0.5f).title(firstMarkers.getTitle())
                .position(firstMarkers.getPosition())
                .icon(firstMarkers.getIcon())
                .snippet(firstMarkers.getSnippet());
        includeMarkers = new ArrayList<MarkerOptions>();
        includeMarkers.add(firstMarkers);
    }

    /** * 添加marker */
    public void addMarker(MarkerOptions markerOptions) {
        includeMarkers.add(markerOptions);// 添加到列表中
    }

    /** * 设置聚合点的中心位置以及图标 */
    public void setpositionAndIcon() {
        int size = includeMarkers.size();

        if (size == 1) {
            return;
        }
        double lat = 0.0;
        double lng = 0.0;

        String snippet = "";
        for (MarkerOptions op : includeMarkers) {
            lat += op.getPosition().latitude;
            lng += op.getPosition().longitude;
            snippet += op.getTitle() + "\n";
        }
        options.position(new LatLng(lat / size, lng / size));// 设置中心位置为聚集点的平均位置
        options.title("聚合点");
        options.snippet(snippet);

        int iconType = size / 10;

        switch (iconType) {
        case 0:
            int identifier = activity.getResources().getIdentifier("marker_cluster_10", "drawable", "com.ez360.cd2016");
            options.icon(BitmapDescriptorFactory
                    .fromBitmap(getViewBitmap(getView(size,
                            identifier))));
            break;
        case 1:
            int identifier_2 = activity.getResources().getIdentifier("marker_cluster_20", "drawable", "com.ez360.cd2016");
            options.icon(BitmapDescriptorFactory
                    .fromBitmap(getViewBitmap(getView(size,
                            identifier_2))));
            break;
        case 2:
            int identifier_3 = activity.getResources().getIdentifier("marker_cluster_30", "drawable", "com.ez360.cd2016");
            options.icon(BitmapDescriptorFactory
                    .fromBitmap(getViewBitmap(getView(size,
                            identifier_3))));
            break;
        case 3:
            int identifier_30 = activity.getResources().getIdentifier("marker_cluster_30", "drawable", "com.ez360.cd2016");
            options.icon(BitmapDescriptorFactory
                    .fromBitmap(getViewBitmap(getView(size,
                            identifier_30))));
            break;
        case 4:
            int identifier_50 = activity.getResources().getIdentifier("marker_cluster_50", "drawable", "com.ez360.cd2016");
            options.icon(BitmapDescriptorFactory
                    .fromBitmap(getViewBitmap(getView(size,
                            identifier_50))));
            break;
        default:
            int identifier_100 = activity.getResources().getIdentifier("marker_cluster_100", "drawable", "com.ez360.cd2016");
            options.icon(BitmapDescriptorFactory
                    .fromBitmap(getViewBitmap(getView(size,
                            identifier_100))));
            break;
        }
    }

    public LatLngBounds getBounds() {
        return bounds;
    }

    public MarkerOptions getOptions() {
        return options;
    }

    public void setOptions(MarkerOptions options) {
        this.options = options;
    }

    public View getView(int carNum, int resourceId) {
        View view = activity.getLayoutInflater().inflate(
                R.layout.my_car_cluster_view, null);
        TextView carNumTextView = (TextView) view.findViewById(R.id.my_car_num);
        RelativeLayout myCarLayout = (RelativeLayout) view
                .findViewById(R.id.my_car_bg);
        myCarLayout.setBackgroundResource(resourceId);
        carNumTextView.setText(String.valueOf(carNum));
        return view;
    }

    /** * 把一个view转化成bitmap对象 */
    public static Bitmap getViewBitmap(View view) {
        view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }
}

主要代码,有问题的朋友可以交流~

点赞