在地图android studio中添加多个集群标记

时间:2017-04-17 08:47:26

标签: android google-maps dictionary markerclusterer

我按照教程cluster google map添加我的项目,然后运行。 但是现在我在地图中有两个类型项,所以我想为每个类型项创建两个集群管理器。 我搜索并查看multiple cluster manager,但是当我无法在setOnCameraChangeListener()方法中添加onCameraChange时。 如何在地图中添加多个群集? 非常感谢你!

 @Override
    public void onMapReady(GoogleMap googleMap) {
        isMapReady = true;
        map = googleMap;
        map.getUiSettings().setZoomControlsEnabled(false);
        map.getUiSettings().setMyLocationButtonEnabled(false);
        map.getUiSettings().setCompassEnabled(false);
        Gps lastGps = ((PagerActivity) getActivity()).getLastGPS();

        MarkerOptions markerOptions = new MarkerOptions()
                .position(new LatLng(lastGps.getLat(), lastGps.getLng()))
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.radar_boy));
        map.addMarker(markerOptions);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lastGps.getLat(), lastGps.getLng()), 12));
        clusterManager = new ClusterManager<MarkerItem>(getContext(), map);
        clusterManager1 = new ClusterManager<MarkerItem>(getContext(), map);
        map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
                clusterManager.onCameraIdle();
                clusterManager1.onCameraIdle();
            }
        });

        ClusterRenderer clusterRenderer = new ClusterRenderer(getContext(), map, clusterManager);
        clusterManager.setRenderer(clusterRenderer);
        ClusterRenderer clusterRenderer1 = new ClusterRenderer(getContext(), map, clusterManager1);
        clusterManager1.setRenderer(clusterRenderer1);
        addMarker();
    }

    private void addMarker() {       
        List<MyItem> itemsGold = new ArrayList<>();// list item type 1
        List<MyItem> itemsGoldOre = new ArrayList<>();// list item type 2
		....// add item in two list
        // add item in map
        if (isLoadDataComplete && isMapReady) {
            for (Point point : listGold) {
                DetailItemApi.DetailItem detailItem = detailItemUtil.getDetailItem(point.type);
                Glide.with(getActivity())
                        .load(baseUrl + detailItem.img.getRadar_s())
                        .asBitmap()
                        .into(new SimpleTarget<Bitmap>(100, 100) {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                MarkerOptions markerOptions = new MarkerOptions()
                                        .position(new LatLng(point.gps.getLat(), point.gps.getLng()))
                                        .icon(BitmapDescriptorFactory.fromBitmap(resource));
                                MarkerItem markerItem = new MarkerItem(markerOptions);
                                clusterManager.addItem(markerItem);
                            }
                        });
            }

            for (Point point : listGoldOre) {
                DetailItemApi.DetailItem detailItem = detailItemUtil.getDetailItem(point.type);
                Glide.with(getActivity())
                        .load(baseUrl + detailItem.img.getRadar_s())
                        .asBitmap()
                        .into(new SimpleTarget<Bitmap>(100, 100) {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                MarkerOptions markerOptions = new MarkerOptions()
                                        .position(new LatLng(point.gps.getLat(), point.gps.getLng()))
                                        .icon(BitmapDescriptorFactory.fromBitmap(resource));
                                MarkerItem markerItem = new MarkerItem(markerOptions);
                                clusterManager1.addItem(markerItem);
                            }
                        });
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

使用MarkerManager

 @Override
public void onMapReady(GoogleMap googleMap) {
    isMapReady = true;
    map = googleMap;
    map.getUiSettings().setZoomControlsEnabled(false);
    map.getUiSettings().setMyLocationButtonEnabled(false);
    map.getUiSettings().setCompassEnabled(false);
    Gps lastGps = ((PagerActivity) getActivity()).getLastGPS();

    MarkerOptions markerOptions = new MarkerOptions()
            .position(new LatLng(lastGps.getLat(), lastGps.getLng()))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.radar_boy));
    map.addMarker(markerOptions);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lastGps.getLat(), lastGps.getLng()), 12));

    MarkerManager markerManager = new MarkerManager(map);

    clusterManager = new ClusterManager<MarkerItem>(getContext(), map, 
  markerManager);
        clusterManager1 = new ClusterManager<MarkerItem>(getContext(), map, markerManager);

    map.setOnMarkerClickListener(markerManager);
    map.setOnInfoWindowClickListener(markerManager);

        map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
                clusterManager.onCameraIdle();
                clusterManager1.onCameraIdle();
            }
        });

        ClusterRenderer clusterRenderer = new ClusterRenderer(getContext(), map, clusterManager);
        clusterManager.setRenderer(clusterRenderer);
        ClusterRenderer clusterRenderer1 = new ClusterRenderer(getContext(), map, clusterManager1);
        clusterManager1.setRenderer(clusterRenderer1);
        addMarker();
        clusterManager.cluster();
        clusterManager1.cluster();
    }