<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>自定义样式信息窗体</title>
<link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css" />
<!--设置信息窗体的样式-->
<style type="text/css">
.info {
border: solid 1px silver;
}
div.info-top {
position: relative;
background: none repeat scroll 0 0 #F9F9F9;
border-bottom: 1px solid #CCC;
border-radius: 5px 5px 0 0;
}
div.info-top div {
display: inline-block;
color: #333333;
font-size: 14px;
font-weight: bold;
line-height: 31px;
padding: 0 10px;
}
div.info-top img {
position: absolute;
top: 10px;
right: 10px;
transition-duration: 0.25s;
}
div.info-top img:hover {
box-shadow: 0px 0px 5px #000;
}
div.info-middle {
width: 480px;
font-size: 12px;
padding: 6px;
line-height: 20px;
}
div.info-bottom {
height: 0px;
width: 100%;
clear: both;
text-align: center;
}
div.info-bottom img {
position: relative;
z-index: 104;
}
.info-middle img {
float: left;
margin-right: 6px;
}
.fontColor {
color: rgba(135, 135, 135, 1);
width: 80px;
height: 27px;
color: #70AFFF;
}
.fontColor_ {
color: red;
}
.content-window-card {
position: relative;
box-shadow: none;
bottom: 0;
left: 0;
width: auto;
padding: 0;
}
.content-window-card p {
height: 2rem;
}
</style>
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.6&7f2c690dc8aecb3c4a8b7616798297d0&plugin=AMap.MarkerClusterer"></script>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">
var map = new AMap.Map("container", {
resizeEnable: true,
view: new AMap.View2D({
center: new AMap.LngLat(121.414411, 31.060719),
zoom: 15
})
});
var marker = new AMap.Marker({
map: map,
position: [121.414411, 31.060719]//点所在位置
});
//实例化信息窗体
//标题头
var title = '1234567';
let content = `<table border='0' class='define-table' style='width:100%;'>
<tr>
<td class='fontColor'>车辆Id</td>
<td class='content'>13598786666</td>
<td class='fontColor'>卫星时间</td>
<td width='140px;' class='content'>2022-07-01 19:05:17</td>
</tr>
<tr>
<td class='fontColor'>行驶速度</td>
<td class='content'>0 km/h</td>
<td class='fontColor'>行驶里程</td>
<td class='content'>0/0km</td>
</tr>
<tr>
<td class='fontColor'>当前报警</td>
<td colspan='3' class='fontColor_'> 视频丢失(2,3,4,5,6,7,8) 硬盘故障SD卡2异常</td>
</tr>
<tr>
<td class='fontColor'>地理位置</td>
<td colspan='3' class='content'>全方位科技园 上海市闵行区都会路1699号</td>
</tr>
<tr>
<td class='fontColor'>ACC状态</td>
<td class='content'>ACC 开</td>
<td class='fontColor'>车辆操作</td>
<td colspan='3'>
<img src='https://webapi.amap.com/images/close2.gif' title='实时预览' style='width:24px;height:24px;'>
<img src='https://webapi.amap.com/images/close2.gif' title='轨迹回放' style='width:24px;height:24px;'>
<img src='https://webapi.amap.com/images/close2.gif' title='文本下发' style='width:24px;height:24px;'>
</td>
</tr>
</table>`;
//创建信息窗体
var infoWindow = new AMap.InfoWindow({
isCustom: true, //是否自定义信息窗体
content: createInfoWindow(title, content), //调用创建信息窗体的方法--信息窗体的内容
offset: new AMap.Pixel(16, -45) //偏移量
});
//如果希望的是点击标记才 出现这个信息窗口,那把 下面的注释去掉即可
AMap.event.addListener(marker, 'click', function () { //监听点标记的点击事件
infoWindow.open(map, marker.getPosition()); //信息窗体打开
});
infoWindow.open(map, marker.getPosition()); //信息窗体打开
//构建自定义信息窗体
function createInfoWindow(title, content) {
//info 为 信息窗体
var info = document.createElement("div");
info.className = "info";
//可以通过下面的方式修改自定义窗体的宽高
//info.style.width = "400px";
// 定义顶部标题
var top = document.createElement("div");
var titleD = document.createElement("div");
var closeX = document.createElement("img");
top.className = "info-top";
titleD.innerHTML = title;
closeX.src = "http://webapi.amap.com/images/close2.gif";
closeX.onclick = closeInfoWindow; //点击右上角的x可以关闭该信息窗体
top.appendChild(titleD);
top.appendChild(closeX);
info.appendChild(top); //信息窗体增加顶部的div
// 定义中部内容
var middle = document.createElement("div");
middle.className = "info-middle";
middle.style.backgroundColor = 'white';
middle.innerHTML = content;
info.appendChild(middle); //信息窗体增加中部的div
// 定义底部内容
var bottom = document.createElement("div");
bottom.className = "info-bottom";
bottom.style.position = 'relative';
bottom.style.top = '0px';
bottom.style.margin = '0 auto';
var sharp = document.createElement("img");
sharp.src = "http://webapi.amap.com/images/sharp.png";
bottom.appendChild(sharp);
info.appendChild(bottom); //信息窗体增加底部的div
return info;
}
//关闭信息窗体
function closeInfoWindow() {
map.clearInfoWindow();
}
</script>
</body>
</html>
评论0