【发布时间】:2022-01-07 15:36:00
【问题描述】:
我的着陆页上有一张 Javascript 地图。 我设置了几个标记,并让它们在点击时显示在 Windows 中。
当您单击标记时,它会显示信息窗口。问题是没有办法关闭其他的。
我可以 closehere 曾经是一个 .markers 属性来获取所有标记,因此我可以在 for 循环中关闭每个标记。 这不再存在。
我尝试过使用共享信息窗口。它不起作用,因为无法在标记的点击事件中设置内容。
这看起来应该很简单。
有什么想法吗?
参考代码:
function addMarker({maps,map,position,html,image,center,allowClick,location}){
// Create InfoWindow
if (infowindow) {
infowindow.close();
}
const newHtml=` <div
style={{backgroundColor:"white",width:"300px",height:"250px",borderRadius:"8px",boxShadow:"0 2px 7px 1px rgb(0 0 0 / 30%",boxSizing: "border-box",
overflow: "hidden",padding:"12px"}}
>
${location.name}
${location.phone}
${location.address}
</div>`
infowindow = new maps.InfoWindow({
content: newHtml,
zIndex: 2,
maxWidth: 500,
});
// Add Marker with infowindow
const marker = new maps.Marker({
map,
position,
icon: image,
infowindow,
});
if(allowClick){
// Add Click Event to Marker
maps.event.addListener(marker, "click", function(arg) {
//NEED TO CLOSE OTHERS HERE!!!
// Open Info Window
infowindow.open(map, marker);
// Center Map on Marker
if(center){
map.setCenter(marker.getPosition());
}
});
}
return marker;
}
【问题讨论】:
标签: reactjs google-maps google-maps-api-3 google-maps-markers google-maps-api-2