代码
const map = new BMap.Map('map');
var point = new BMap.Point(116.404, 39.925);
map.centerAndZoom(point, 15);
var marker = new BMap.Marker(point); // 创建标注
map.addOverlay(marker);
const content = `
<div style="font-family: sans-serif; padding: 4px 0;">
<div style="display: flex; align-items: center; gap: 6px; margin-bottom: 8px;">
<span style="font-size: 15px; font-weight: bold; color: #333;">餐厅名称</span>
<span style="background:#ff6b35; color:#fff; font-size:11px; padding:1px 6px; border-radius:3px;">营业中</span>
</div>
<div style="font-size: 13px; color: #555; line-height: 1.8;">
<div> 地址:北京市xxx</div>
<div> 电话:010-12345678</div>
<div>营业时间:09:00 - 22:00</div>
</div>
<div style="display: flex; gap: 8px; margin-top: 10px;">
<button onclick="alert('导航')" style="
flex: 1; padding: 6px; background: #4a90e2;
color: white; border: none; border-radius: 4px; cursor: pointer;">
导航前往
</button>
<button onclick="alert('详情')" style="
flex: 1; padding: 6px; background: white;
color: #4a90e2; border: 1px solid #4a90e2;
border-radius: 4px; cursor: pointer;">
查看详情
</button>
</div>
</div>`;
const infoWindow = new BMap.InfoWindow(content, {
width: 280,
offset: new BMap.Size(0, -40),
height: 0, // 0 表示自适应高度
title: '', // 不设标题时留空
});
marker.addEventListener('click', () => {
map.openInfoWindow(infoWindow, point);
});
// 页面初始化时打开信息窗口
map.openInfoWindow(infoWindow, point);
展示如何创建自定义样式的信息窗口
预览