我想在clustering中创建MapView,但是在群集中创建自己的标记时遇到了问题。这是我的MainActivity.class
Image

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.maps.android.clustering.ClusterManager;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

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

private void setUpMapIfNeeded() {
    if (mMap == null) {
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

    }
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    //LatLng sydney = new LatLng(-34, 151);

   //ICE CREAM
    LatLng oen = new LatLng(-7.980697, 112.630356);
    mMap.addMarker(new MarkerOptions()
            .position(oen)
            .title("Toko OEN").icon(BitmapDescriptorFactory.fromResource(R.drawable.ice)));

    LatLng uni = new LatLng(-7.975091, 112.636666);
    mMap.addMarker(new MarkerOptions()
            .position(uni)
            .title("Unicone").icon(BitmapDescriptorFactory.fromResource(R.drawable.ice)));

    LatLng bvgil = new LatLng(-7.968335, 112.627971);
    mMap.addMarker(new MarkerOptions()
            .position(bvgil)
            .title("Bvgil").icon(BitmapDescriptorFactory.fromResource(R.drawable.ice)));

    //BAKSO
    LatLng cakman = new LatLng(-7.966293, 112.637497);
    mMap.addMarker(new MarkerOptions()
            .position(cakman)
            .title("Bakso Cak Man").icon(BitmapDescriptorFactory.fromResource(R.drawable.bakso)));

    LatLng presiden = new LatLng(-7.962405, 112.636818);
    mMap.addMarker(new MarkerOptions()
            .position(presiden)
            .title("Bakso Presiden").icon(BitmapDescriptorFactory.fromResource(R.drawable.bakso)));

    LatLng damas = new LatLng(-7.938407, 112.625234);
    mMap.addMarker(new MarkerOptions()
            .position(damas)
            .title("Bakso Damas").icon(BitmapDescriptorFactory.fromResource(R.drawable.bakso)));

    LatLng dul = new LatLng(-7.978703, 112.637199);
    mMap.addMarker(new MarkerOptions()
            .position(dul)
            .title("Bakso P.Dulmanan").icon(BitmapDescriptorFactory.fromResource(R.drawable.bakso)));

    LatLng priangan = new LatLng(-7.946504, 112.658021);
    mMap.addMarker(new MarkerOptions()
            .position(priangan)
            .title("Bakso Priangan").icon(BitmapDescriptorFactory.fromResource(R.drawable.bakso)));

    //SOTO
    LatLng sobas = new LatLng(-7.978214, 112.637015);
    mMap.addMarker(new MarkerOptions()
            .position(sobas)
            .title("Soto Basket").icon(BitmapDescriptorFactory.fromResource(R.drawable.rawonsoto)));

    LatLng soayam = new LatLng(-7.988218, 112.625781);
    mMap.addMarker(new MarkerOptions()
            .position(soayam)
            .title("Soto Ayam Lombok").icon(BitmapDescriptorFactory.fromResource(R.drawable.rawonsoto)));

    LatLng sonjar = new LatLng(-7.945175, 112.629470);
    mMap.addMarker(new MarkerOptions()
            .position(sonjar)
            .title("Soto Banjar").icon(BitmapDescriptorFactory.fromResource(R.drawable.rawonsoto)));

    //RAWON
    LatLng nguling = new LatLng(-7.983949, 112.633936);
    mMap.addMarker(new MarkerOptions()
            .position(nguling)
            .title("Rawon Nguling").icon(BitmapDescriptorFactory.fromResource(R.drawable.rawonsoto)));

    LatLng rampal = new LatLng(-7.974173, 112.638356);
    mMap.addMarker(new MarkerOptions()
            .position(rampal)
            .title("Rawon Rampal").icon(BitmapDescriptorFactory.fromResource(R.drawable.rawonsoto)));

    LatLng tessy = new LatLng(-7.980073, 112.637236);
    mMap.addMarker(new MarkerOptions()
            .position(tessy)
            .title("Rawon Tessy").icon(BitmapDescriptorFactory.fromResource(R.drawable.rawonsoto)));

    //ZOOM
    LatLng zoom = new LatLng(-7.966658, 112.631923);

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(zoom, 13));

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    mMap.setMyLocationEnabled(true);
}
}


最佳答案

您实际上根本没有使用ClusterManager。现在,您只是在地图上添加了图钉-仅导入ClusterManager并不会创建聚类行为。

您需要创建您的ClusterManager

mClusterManager = new ClusterManager<>(getActivity(), mMap);


创建自己的实现ClusterItem接口的集群项目类:

public static class MyClusterItem implements ClusterItem {

    private final LatLng mPosition;

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

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


然后将该项目添加到ClusterManager

mClusterManager.addItem(new MyClusterItem(locationDetail));


请注意,将项目添加到ClusterManager意味着您无需将标记添加到实际的地图对象。

您还需要在某个时候调用mClusterManager.cluster()方法。触发cluster()事件时,群集管理器的默认行为将调用onCameraIdle,但是您可能需要根据使用情况手动执行此操作。

10-05 18:22