How to make a marker appear or disappear based on

2019-06-11 15:12发布

问题:

We all know that some of the predefined landmarks on Google Maps does not appear on a lower zoom level, but on a higher zoom level, it suddenly appears. I would like to know If I can make a customized marker not appear at lower zoom levels, then appear at higher ones.

EDIT: Here is a snippet of my code.

 // Changing marker icon
   marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.gasbig));


   // adding marker
  map.addMarker(marker);

  //position on Center

  CameraPosition cameraPosition = new CameraPosition.Builder().target(
          new LatLng(14.635356, 121.03272914)).zoom(16).build();

  map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

  map.setOnCameraChangeListener(new OnCameraChangeListener() {

        @Override
        public void onCameraChange(CameraPosition arg0) {
            if(arg0.zoom > 7){
                marker.visible(true);

            }


        }
    });

I tried the suggestion of MaciejGórski but the marker still appears on all zoom levels. I'm sorry about the question I'm still an android newbie.

Thanks in advance.

回答1:

You can do that for any Marker you want: call setVisible in OnCameraChangeListener.onCameraChange callback with true or false depending on CameraPosition.zoom value.

Edit after question edit:

You need to keep a reference to Marker instead of MarkerOptions:

// adding marker
marker = map.addMarker(markerOptions);

and call setVisible on that marker:

    @Override
    public void onCameraChange(CameraPosition cameraPosition) {
        marker.setVisible(cameraPosition.zoom > 7);
    }

Note: setVisible is always called there, but this might not be optimal when using many Markers.



回答2:

You could possibly do it by modifying my answer to: Android Maps v2 - animate camera to include most markers

Otherwise using Android Maps Extensions might be a good choice. No experince with your specific needs though.

Just realized I might have misunderstood the question. Thought you meant your own markers. Nevertheless have a look at the extensions library. Could very well be that they have something useful.