基本上标题是什么。当我删除搜索框中的所有内容时,如何取消搜索?
对其作用的一个小描述:
但我想添加的内容(以及目前无效的内容)是:如果我从搜索框中移除所有内容,我希望所有标记再次可见。但是,它不起作用。
搜索框中处理文字更改的代码:
google.maps.event.addListener(searchBox, 'places_changed', function() {
alert('input: ' + input.value);
var newMarkers = []; //a temp variable to store the markers that should be visible
if ((places.length <= 0) || (input.length <= 0) || (input.value == "") || (input.value == "Search Box")) {
//here I set all markers to visible again(newMarkers = markers)
} else {
//here I push all markers that fit the criteria into the newMarkers variable
}
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < newMarkers.length; i++) {
bounds.extend(newMarkers[i].getPosition());
}
markerCluster.clearMarkers();
markerCluster.addMarkers(newMarkers);
map.fitBounds(bounds);
});
问题是代码永远不会进入第一个 if 。实际上,如果我在搜索框中删除整个文本并点击 Enter ,它就永远不会进入 function 。
答案 0 :(得分:0)
根据文档,当用户选择查询时,会触发SearchBox places_changed
事件&#34;
如果用户删除了内容,则他们不能选择任何内容。相反,您可以自己触发事件:https://developers.google.com/maps/documentation/javascript/reference#event
if (input.value.length === 0) {
google.maps.event.trigger(searchBox, 'places_changed');
}