问题描述:

I'm trying to implement a cluster marker on my map, and it is behaving a little strange, first, it shows me the cluster marker with the right number of markers, but when I zoom out to join other markers it generates another cluster marker which I don't know where it is coming from and why it is showing on the map, I`ll add some image to explain it better:

Here is the image with zoom in, as you can see, I have a cluster marker with 8 points and another one alone, so when I zoom out it should give me one clusterMarker with 9 points, but look what happens when I zoom out:

What that cluster marker with 7 points is doing there?

here is my code:

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;

}

}

}

网友答案:

There seems to be an issue in this code:

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

You should be adding these two things in there:

getMap().setOnCameraChangeListener(mClusterManager);

and

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

Here's an example from the documentation: https://developers.google.com/maps/documentation/android/utility/marker-clustering#simple

相关阅读:
Top