Robin Royal Robin Royal - 1 year ago 452
Android Question

Android Google Map show markers on zoom change

Guys I have to show markers on google maps with zoom.When user zoom in or zoom out markers will be visible covering the map area which is on the screen.How can I do this using Android google map?

Answer Source

Okay so you can use a CameraChangeListener:

private HashMap<Integer, Marker> courseMarkers = new HashMap<Integer, Marker>();
//all your visible markers
ArrayList<Item> yourMarkerList = new ArrayList<Item>();
//method to add all your markers with unique ids
addItemsToMap(); //first call to initially show the markers you want

googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {

private float currentZoom = -1; //keep track of your current zoom level

public void onCameraChange(CameraPosition camera) {
    if (camera.zoom != currentZoom){
        currentZoom = camera.zoom;
        //here you will then check your markers

You will need a class Item that has a variable for a unique int id and a MarkerOptions

private void addItemsToMap(List<Item> items)
if(this.mMap != null)
    //This is the current user-viewable region of the map
    LatLngBounds bounds = this.mMap.getProjection().getVisibleRegion().latLngBounds;

    //Loop through all the items that are available to be placed on the map
    for(Item m : item) 

        //If the item is within the the bounds of the screen
            //If the item isn't already being displayed
                //Add the Marker to the Map and keep track of it with the HashMap
                //getMarkerForItem just returns a MarkerOptions object
                this.courseMarkers.put(item.getId(), this.googleMap.addMarker(item.getMarker())); //getmarkerforitem

        //If the marker is off screen
            //If the course was previously on screen
                //1. Remove the Marker from the GoogleMap

                //2. Remove the reference to the Marker from the HashMap


This should do it

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download