Show/Hide Bookmarks as Zoom on Android map

Asked

Viewed 2,999 times

3

I’m developing an android app that will contain many bookmarks on the map, and in order not to get too messy I wish that if I was zooming out the markups would go into Hide, I tried the following code:

@Override
public void onCameraChange(CameraPosition cameraPosition) {
    if (cameraPosition.zoom > 7) {
        Toast.makeText(this,"teste",Toast.LENGTH_LONG).show();
        marker.setVisible(true);
    }else{
        marker.setVisible(false);
   }
}

When the zoom is greater than 7 would be for it to show, but does not work.

2 answers

3


To Google Maps Android API Utility Library offers, among others, classes for manage groups of bookmarks.
Grouping the markers, you can put a large number of markers on a map without making the map hard to read.

The use of api is easy, are 5 steps to implement:

1 - Implement a Clusteritem to represent each of the markers on the map.

public class Marcador implements ClusterItem {
    private final LatLng mPosition;

    public Marcador(double lat, double lng) {
        mPosition = new LatLng(lat, lng);
    }

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

2 - Use a Clustermanager to group and manage bookmarks(Clusteritem).

private ClusterManager<MyItem> mClusterManager;
mClusterManager = new ClusterManager<Marcador>(this, getMap());

3 - Attribute the Clustermanager at the OnCameraChangeListener() of the map.

getMap().setOnCameraChangeListener(mClusterManager);

4 - If you want to add specific functionality in response to a bookmark click, assign the Clustermanager at the OnMarkerClickListener() of the map.

getMap().setOnMarkerClickListener(mClusterManager);

5 - Add each of the markers(Clusteritem) at the Clustermanager

double lat = 51.5145160;
double lng = -0.1270060;
Marcador marcador = new Marcador(lat, lng);
mClusterManager.addItem(marcador);  

//Adicionar outros marcadores
......
......

Done, the Clustermanager will manage the markers, ensuring that the map is always easy to read.

To have the Api available add the following dependency to Gradle build file of the application:

dependencies {
    compile 'com.google.maps.android:android-maps-utils:0.4+'
}

Font: Google Maps Android Marker Clustering Utility

  • I don’t understand exactly how I should group Clustermanager can help me in this part?

  • I will try to help but it is necessary for you to say, perhaps by asking another question, what is your specific question.

  • Okay, I have the map Fragment class, main Activity, Clusteritem class, it’s unclear if Clustermanager tbm is a class. if you could clarify a little or quote some examples I would be very grateful

  • Yes it is a class (that belongs to the api), see in point 2 how it is instantiated. See here what to do to be able to use.

1

There is a method that is functional, not indicated but sure to disappear with all markers at the same time as the zoom value.

Implement the class with:

public class MapsActivity extends SupportMapFragment implements OnMapReadyCallback GoogleMap.OnCameraChangeListener {

In creating your map:

@Override
public void onMapReady(GoogleMap googleMap) {
    mapa = googleMap;
    mapa.setOnCameraChangeListener(getOnCameraChangeListener());
}

Implements and methods:

public GoogleMap.OnCameraChangeListener getOnCameraChangeListener()
{
    return  new GoogleMap.OnCameraChangeListener()
    {
    @Override
    public void onCameraChange(CameraPosition position) {

        if(position.zoom >7)
            marker.setVisible(true);
        else
            marker.setVisible(false);

    }};
}

@Override
public void onCameraChange(CameraPosition cameraPosition) {

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.