问题描述
我正在尝试创建Google Maps版本,并希望在地图中插入标记后清理SearchBox。
示例:我在地图上的意大利上放置了一个标记,但名称“意大利”仍保留在SearchBox中。为此,我需要手动清除搜索栏。
我的代码是:
function initAutocomplete() {
const map = new google.maps.Map(document.getElementById("map"),{
center: {
lat: -15,lng: -55
},zoom: 5,mapTypeId: "roadmap"
}); // Create the search Box and link it to the UI element.
const input = document.getElementById("pac-input");
const 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",() => {
searchBox.setBounds(map.getBounds());
});
let markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener("places_changed",() => {
const places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// For each place,get the icon,name and location.
const bounds = new google.maps.LatLngBounds();
places.forEach(place => {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
const icon = {
url: place.icon,size: new google.maps.Size(100,100),origin: new google.maps.Point(0,0),anchor: new google.maps.Point(17,34),scaledSize: new google.maps.Size(25,25)
};
// Create a marker for each place.
markers.push(
new google.maps.Marker({
map,icon,title: place.name,position: place.geometry.location
})
);
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}
有人可以帮助我吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)