其他分享
首页 > 其他分享> > android – 放大时不会隐藏群集标记

android – 放大时不会隐藏群集标记

作者:互联网

我正在尝试在我的地图上实现一个群集标记,它表现得有些奇怪,首先,它向我展示了群集标记,但也向我显示了点标记,这不应该发生,当我放大时群集标记仍然显示,我会添加一些图像来更好地解释它.

public class MapaViagem extends FragmentActivity {

    private GoogleMap googleMap;
    private String rm_IdViagem;
    private List<ClienteModel> mClienteModel = new ArrayList<ClienteModel>();
    private List<EnderecoModel> mEnderecoModel = new ArrayList<EnderecoModel>();
    private ViagemModel mViagemModel = new ViagemModel();
    private ClusterManager<MyItem> mClusterManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.maps);
         ArrayList<LatLng> coordList = new ArrayList<LatLng>();

        try {

            Bundle parametros = getIntent().getExtras();
            rm_IdViagem = parametros.getString("id_viagem");

            Repositorio ca = new Repositorio(this);
            mViagemModel = ca.getViagemPorId(Integer.valueOf(rm_IdViagem));

            Repositorio cl = new Repositorio(this);
            mClienteModel = cl.getClientesViagem(Integer.valueOf(rm_IdViagem));


            System.out.println("TEM CLIENTE " + mClienteModel.size());

            if(mClienteModel != null) {

                for (int i = 0; i < mClienteModel.size(); i++) {


                    Repositorio mRepositorio = new Repositorio(this);
                    mEnderecoModel = mRepositorio.getListaEnderecosDoCliente(Integer.valueOf(mClienteModel.get(i).getClientes_id()));


                    for (int j = 0; j < mEnderecoModel.size(); j++) {
                        // Loading map
                        initilizeMap();
                        // Changing map type
                        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

                        // Showing / hiding your current location
                        googleMap.setMyLocationEnabled(true);

                        // Enable / Disable zooming controls
                        googleMap.getUiSettings().setZoomControlsEnabled(true);

                        // Enable / Disable my location button
                        googleMap.getUiSettings().setMyLocationButtonEnabled(true);

                        // Enable / Disable Compass icon
                        googleMap.getUiSettings().setCompassEnabled(true);

                        // Enable / Disable Rotate gesture
                        googleMap.getUiSettings().setRotateGesturesEnabled(true);

                        // Enable / Disable zooming functionality
                        googleMap.getUiSettings().setZoomGesturesEnabled(true);


                        final float latitude = Float.parseFloat(mEnderecoModel.get(j).getLatitude());
                        final float longitude = Float.parseFloat(mEnderecoModel.get(j).getLongitude());


                        coordList.add(new LatLng(latitude, longitude));


                        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 10));



                        mClusterManager = new ClusterManager<MyItem>(MapaViagem.this, googleMap);


                        mClusterManager.setRenderer(new MyClusterRenderer(MapaViagem.this, googleMap, mClusterManager));

                        googleMap.setOnCameraChangeListener(mClusterManager);
                        googleMap.setOnMarkerClickListener(mClusterManager);


                        addItems(coordList);

                    }


                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public class MyClusterRenderer extends DefaultClusterRenderer<MyItem> {

        public MyClusterRenderer(Context context, GoogleMap map,
                                 ClusterManager<MyItem> clusterManager) {
            super(context, map, clusterManager);
        }

        @Override
        protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
            super.onBeforeClusterItemRendered(item, markerOptions);



//            item.setTitle(mClienteModel.get(i));
            markerOptions.title(item.getTitle());
        }

        @Override
        protected void onClusterItemRendered(MyItem clusterItem, Marker marker) {
            super.onClusterItemRendered(clusterItem, marker);

            //here you have access to the marker itself
        }

 @Override
        protected boolean shouldRenderAsCluster(Cluster<MyItem> cluster) {
            return cluster.getSize() > 3; // when count of markers is more than 3, render as cluster
        }
    }


    private void addItems(List<LatLng> markers) {

        for (int i = 0; i < markers.size(); i++) {
            MyItem offsetItem = new MyItem(markers.get(i));
            mClusterManager.addItem(offsetItem);

        }
        mClusterManager.cluster();
    }



    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Não foi possível carregar o mapa", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }


}

模型:

public class MyItem implements ClusterItem {

    private LatLng mPosition;
    private String title;

    public MyItem(LatLng position){
        mPosition = position;
    }

    @Override
    public LatLng getPosition() {
        return mPosition;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

解决方法:

首先,你需要定义,当你的ClusterManager应该聚类时,你应该覆盖MyClusterRenderer中的方法:

@Override
protected boolean shouldRenderAsCluster(Cluster<MyItem> cluster) {
    return cluster.getSize() > 3; // when count of markers is more than 3, render as cluster
}

其次,当您使用标记进行一些更改(例如添加,删除,更改标题,图标,位置)时,应调用cluster()方法.所以你的addItems方法应如下所示:

private void addItems(List<LatLng> markers) {

    for (int i = 0; i < markers.size(); i++) {
        MyItem offsetItem = new MyItem(markers.get(i));
        mClusterManager.addItem(offsetItem);
    }
    mClusterManager.cluster();
}

编辑:

我再一次看你的代码,发现你忘了设置聚类算法,它应该是:

    ...
    mClusterManager = new ClusterManager<MyItem>(MapaViagem.this, googleMap);
    mClusterManager.setAlgorithm(new GridBasedAlgorithm<MyItem>());

    mClusterManager.setRenderer(new MyClusterRenderer(MapaViagem.this, googleMap, mClusterManager));
    ...

标签:google-maps-api-2,android,google-maps,google-maps-api-3,markerclusterer
来源: https://codeday.me/bug/20190830/1767102.html