在所需位置具有标记的群集

时间:2017-01-24 20:57:12

标签: android

我是Android新手编程的新手,我正在开发一个应用程序,它显示了我通过代码给你的一些标记,在添加群集时,它标记了一些默认点。 我想知道如何更改群集中的默认点并将标记添加到我想要的点?

private void addItems() {

    double lat = 3.424014;
    double lng = -76.536218;

    for (int i = 0; i < 5; i++) {

        double offset = i / 60d;
        lat = lat + offset;
        lng = lng + offset;
        MyItem offsetItem = new MyItem(lat, lng);
        mClusterManager.addItem(offsetItem);

    }
}

@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);
    btnclute=(Button)findViewById(R.id.btn_cluster);

    //cluster

    btnclute.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(3.422504, -76.538128), 13));
            mClusterManager = new  ClusterManager<MyItem>(getBaseContext(), mMap);

            mMap.setOnCameraIdleListener(mClusterManager);
            mMap.setOnMarkerClickListener(mClusterManager);

            addItems();

        }
    });


}

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mapa= googleMap;


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

    //Marker

    //Estadio
    LatLng home = new LatLng(3.429862, -76.541336);
    dato=getResources().getString(R.string.estadio);
    mMap.addMarker(new MarkerOptions().position(home).title(dato)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.estadio)));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(home, 13));

    //Sebastian Belalcazar

    LatLng home1 = new LatLng(3.449151, -76.545185);
    dato1=getResources().getString(R.string.sebas);
    mMap.addMarker(new MarkerOptions().position(home1).title(dato1)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.sebastian)));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(home1, 13));

    //Cristo Rey

    LatLng home2 = new LatLng(3.435855, -76.564840);
    dato2=getResources().getString(R.string.rey);
    mMap.addMarker(new MarkerOptions().position(home2).title(dato2)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.cristo)));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(home2, 13));

    //Tres cruces

    LatLng home3 = new LatLng(3.467800, -76.545473);
    mMap.addMarker(new MarkerOptions().position(home3).title(getResources().getString(R.string.cruces))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.tres)));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(home3, 13));

    //Plaza de Caicedo

    LatLng home5 = new LatLng( 3.451864, -76.532479);
    dato3=getResources().getString(R.string.plaza);
    dato4="-" + getResources().getString(R.string.caicedo);
    dato5="-" + getResources().getString(R.string.pedro);
    dato6="-" + getResources().getString(R.string.palacio);
    mMap.addMarker(new MarkerOptions().position(home5).title(dato3)
            .snippet(dato4 + "\n" +
            dato5 + "\n" +
            dato6)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.plaza)));
    mapa.moveCamera(CameraUpdateFactory.newLatLngZoom(home5, 13));

1 个答案:

答案 0 :(得分:0)

您需要设置算法,以便群集不会在网格中对齐,从而变得更加精确。

NonHierarchicalDistanceBasedAlgorithm<ClusterItem> algo = new NonHierarchicalDistanceBasedAlgorithm<>();
mClusterManager.setAlgorithm(algo);

//If that isn't enough you can change the latlng
Collection<ClusterItem> col = algo.getItems();
    for (ClusterItem clusterItem : col) {
        clusterItem.getPosition().latitude = yourNewLatitude;
        clusterItem.getPosition().longitude = yourNewLongitude;
    }