自动填充搜索功能在我的地图上运行良好,可以放大搜索的地址并将自定义图钉居中放置,但是图钉在所有缩放级别都保持在那里。我的目标是在用户缩小超过 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);
});
Zoom 在你的 initMap 函数中。只需使用条件语句。