群集标记在缩小Android谷歌地图时计算错误

时间:2014-12-04 16:46:41

标签: android google-maps google-maps-api-3 cluster-computing google-maps-android-api-2

我正在尝试在我的地图上实现一个群集标记,它表现得有点奇怪,首先,它向我展示了具有正确数量标记的群集标记,但是当我缩小以加入其他标记时,它会生成另一个群集标记,我不知道它来自哪里以及为什么它会在地图上显示,我会添加一些图像来更好地解释它:

enter image description here

这是放大的图像,你可以看到,我有一个8点的集群标记和另一个单独的标记,所以当我缩小它应该给我一个9点的clusterMarker,但看看当我发生了什么缩小:

enter image description here

7点的群集标记在那里做什么?

这是我的代码:

public class MapaViagem extends FragmentActivity implements ClusterManager.OnClusterClickListener<MyItem>, ClusterManager.OnClusterItemClickListener<MyItem> {

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


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


        setContentView(R.layout.maps);





        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));


            String waypoints = "waypoints=optimize:true";
            String coordenadas = "";

            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);
                        // googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                        // googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                        // googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                        // googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);

                        // 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);


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


                        coordenadas += "|" + latitude + "," + longitude;


                        nomes.add(mClienteModel.get(i).getNome());


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




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


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

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


                        mClusterManager.setOnClusterClickListener(this);

                        mClusterManager.setOnClusterItemClickListener(this);








                        mClusterManager.cluster();

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


                    }


                }

                            String sensor = "sensor=false";
                            String params = waypoints + coordenadas + "&" + sensor;
                            String output = "json";
                            String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + params;
                            ReadTask downloadTask = new ReadTask();
                            downloadTask.execute(url);
            }


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

            markerOptions.title(String.valueOf(item.getName()));
        }

        @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() > 1;
        }



    }
}

1 个答案:

答案 0 :(得分:1)

此代码中似乎存在问题:

coordenadas += "|" + latitude + "," + longitude; nomes.add(mClienteModel.get(i).getNome()); 
coordList.add(new LatLng(latitude, longitude)); 
mClusterManager = new ClusterManager<MyItem>(MapaViagem.this, googleMap); 
mClusterManager.setRenderer(new MyClusterRenderer(MapaViagem.this, googleMap, mClusterManager)); 
addItems(coordList, nomes);

你应该在这里添加这两件事:

getMap().setOnCameraChangeListener(mClusterManager);

private void addItems() {

    // Set some lat/lng coordinates to start with.
    double lat = 51.5145160;
    double lng = -0.1270060;

    // Add ten cluster items in close proximity, for purposes of this example.
    for (int i = 0; i < 10; i++) {
        double offset = i / 60d;
        lat = lat + offset;
        lng = lng + offset;
        MyItem offsetItem = new MyItem(lat, lng);
        mClusterManager.addItem(offsetItem);
    }

以下是文档中的示例:https://developers.google.com/maps/documentation/android/utility/marker-clustering#simple