Jared Pawlyzyn Jared Pawlyzyn - 3 years ago 231
Android Question

Google Maps Android - Only show markers below a certain zoom level

I've been working on this for the last few weeks with no working answer. Help is appreciated.

I want to only show markers below a certain zoom level. This is not only to prevent lag, but also to prevent clustering of markers. Marker clustering isn't what I'm looking for.

Please help! Here's my java code so far:

package us.wingitapp.jared.wingit;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
import com.google.android.gms.location.places.ui.PlaceSelectionListener;

import java.io.IOException;
import java.util.List;


public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
PlaceAutocompleteFragment placeAutoComplete;
private static final String TAG = "Main Activity";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

placeAutoComplete = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete);
placeAutoComplete.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {

Log.i(TAG, "Place Selected: " + place.getName());
LatLng latLng = place.getLatLng();

//move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11));
}

@Override
public void onError(Status status) {
Log.d("Maps", "An error occurred: " + status);
}
});

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}


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

//initialize markers here

}


}

Answer Source
  • Create a variable to store markers.

    List<Marker> list = new ArrayList<>();

  • Add all markers to it like.

        Marker marker = googleMap.addMarker(new MarkerOptions().position(latlng).title(name).snippet(snippet));
        list.add(marker);
    
  • Then set an OnCameraChangeListener.

        googleMap.setOnCameraChangeListener(new 
        GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {
        for(Marker m:list){
            m.setVisible(cameraPosition.zoom>8);
        //8 here is your zoom level, you can set it as your need.
        }
        }
        });
    

Also, the advantage of initializing all markers in this list is you can get all info of any marker by providing a position and you can also set a spinner to select a marker and move the camera to that marker upon selection in the spinner.(Below code may not be required but not useless at all)

Following code can help (just a copy paste though from my project).

for (Marker m: list) {
            if (m.getTitle().equals(selection)) {
                m.showInfoWindow();
                CameraPosition cameraPosition = new CameraPosition.Builder().target(m.getPosition()).zoom(14).build();
                googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                break;
            }
        }

selection here is a string passed from the onItemClickListener of the spinner and also you can put above code in a method, put method call in onItemClickListener of the spinner and pass the string through this method call.

Still, something missing, feel free to ask.

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