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

前端 未结 1 1386
攒了一身酷
攒了一身酷 2020-12-22 10:31

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 on

相关标签:
1条回答
  • 2020-12-22 10:46
    • 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.

    0 讨论(0)
提交回复
热议问题