如何使集群与自己的标记Android

发布时间: 2017/3/19 18:33:17
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想要 clusteringMapView ,但我有一个问题让我自己到群集的标记。这是我 MainActivity.class图像

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

解决方法 1:

你实际上并没有使用 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() 方法在一些 point 。为群集管理器的默认行为将调用 cluster()onCameraIdle 事件触发,但你可能想要手动执行此操作取决于您的用例。

官方微信
官方QQ群
31647020