如何在缩小时删除/隐藏Google地图搜索图标?

时间:2017-01-19 22:00:57

标签: javascript google-maps google-maps-api-3 google-maps-markers

自动填充搜索功能在我的地图上运行良好并放大到搜索到的地址并将自定义引脚置于中心位置,但引脚在所有缩放级别都保持不变。我的目标是在用户缩小超过18级缩放时删除/隐藏引脚。我的猜测是带有'zoom_changed'的google.maps.event.addListener可能会有效,但我不知道在这个代码中将它放在何处或如何放置。感谢您提前提供任何帮助。

    // Create the search box and link it to the UI element.
    var input = document.getElementById('pac-input');
    var searchBox = new google.maps.places.SearchBox(input);
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

    // Bias the SearchBox results towards current map's viewport.
    map.addListener('bounds_changed', function() {
      searchBox.setBounds(map.getBounds());
    });

    var markersS = [];
    // Listen for the event fired when the user selects a prediction and        
    // retrieve more details for that place.
    searchBox.addListener('places_changed', function() {
      var places = searchBox.getPlaces();

      if (places.length == 0) {
        return;
      }

      // Clear out the old markersS.
      markersS.forEach(function(marker) {
        marker.setMap(null);
      });
      markersS = [];

      // For each place, get the icon, name and location.
      var bounds = new google.maps.LatLngBounds();
      places.forEach(function(place) {
        if (!place.geometry) {
          console.log("Returned place contains no geometry");
          return;
        }
        var icon = {
          url: '.../HOME.png',
          anchor: new google.maps.Point(41.5, 64),
        };

       // Create a marker for each place and also set Zoom Condition.
        google.maps.event.addListener(map, 'zoom_changed', function() {
        var zoomS = map.getZoom();
        if (zoomS>=18) {
            markersS.push(new google.maps.Marker({
            map: map,
            icon: icon,
            title: place.name,
            position: place.geometry.location,
            //animation: google.maps.Animation.DROP
            }));
          }
        else if (zoomS<18) {
            markersS.forEach(function(marker) {
            marker.setMap(null);
            });
            markersS = [];
          }
        });

        if (place.geometry.viewport) {
          // Only geocodes have viewport.
          bounds.union(place.geometry.viewport);
        } else {
          bounds.extend(place.geometry.location);
        }
      });
      map.fitBounds(bounds);

    });

1 个答案:

答案 0 :(得分:-1)

缩放在您的initMap函数中。只需使用条件语句。