直接上代码 !!! 会一步一步进行讲解
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>高德地图标记点信息框克隆和拖拽</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://webapi.amap.com/maps?v=1.4.15&key=8e990b742a2f4e168eee432972a9972c
"></script>
<style type="text/css">
#container {
width: 500px;
height: 500px;
}
.headall {
z-index: 111111;
position: absolute;
background: red;
}
.amap-info-content {
height: 200px;
width: 200px;
}
.active {
display: block;
}
</style>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">
var collectHeatMapPointIndex = 0
var colllist = []
// 地图
map = new AMap.Map('container', {
zoom: 10,//级别
center: [121.479921, 31.286758],//中心点坐标
viewMode: '3D',//使用3D视图
features: ['bg', 'road'],
showIndoorMap: false,
});
var lnglats = [
[116.368904, 39.923423],
[116.382122, 39.921176],
[116.387271, 39.922501],
[116.398258, 39.914600]
];
var infoWindow = new AMap.InfoWindow({ offset: new AMap.Pixel(0, -30) });
for (var i = 0, marker; i < lnglats.length; i++) {
var marker = new AMap.Marker({
position: lnglats[i],
map: map,
title: i
});
marker.content = `
<div class="headall" id="heatMapBox${collectHeatMapPointIndex}">
我是第 (${i + 1})个Marker
<div id="heatMapBox${collectHeatMapPointIndex}" class = 'dd' onclick="lock(this,${i},'heatMapBox${collectHeatMapPointIndex}')"> 21212</div>
</div> `;
marker.on('click', markerClick);
marker.emit('click', { target: marker });
}
function markerClick(e) {
collectHeatMapPointIndex++
if (colllist.length == 4) {
infoWindow.setContent(e.target.content);
infoWindow.open(map, e.target.getPosition());
return
} else {
colllist.push(marker)
infoWindow.setContent(e.target.content);
infoWindow.open(map, e.target.getPosition());
}
}
function lock(e, id, idClass) {
console.log("我要克隆", idClass);
colllist.forEach(item => {
if (item.getTitle() == id) {
// console.log(22);
var oldTop = $('#' + idClass).offset().top;
var oldLeft = $('#' + idClass).offset().left;
if ($('#' + idClass).hasClass("active")) {
console.log("eww");
return
}
$('#container').prepend($('#' + idClass).clone())
item.setLabel({
offset: new AMap.Pixel(0, 0), //设置文本标注偏移量
content: '',
direction: 'top'
});
$('#' + idClass).css('top', oldTop + 'px')
$('#' + idClass).css('left', oldLeft + 'px')
$('#' + idClass).addClass("active")
drag(idClass, 'headall')
}
})
}
map.setFitView();
function drag(fatherBox, sonBox) {
var oWin = document.getElementById(`${fatherBox}`)
var oH2 = document.querySelector("." + sonBox)
var bDrag = false
var disX = (disY = 0)
oH2.onmousedown = function (event) {
var event = event || window.event
bDrag = true
disX = event.clientX - oWin.offsetLeft
disY = event.clientY - oWin.offsetTop
this.setCapture && this.setCapture()
return false
}
document.onmousemove = function (event) {
if (!bDrag) return
var event = event || window.event
var iL = event.clientX - disX
var iT = event.clientY - disY
var maxL = document.documentElement.clientWidth - oWin.offsetWidth
var maxT = document.documentElement.clientHeight - oWin.offsetHeight
iL = iL < 0 ? 0 : iL
iL = iL > maxL ? maxL : iL
iT = iT < 0 ? 0 : iT
iT = iT > maxT ? maxT : iT
oWin.style.marginTop = oWin.style.marginLeft = 0
oWin.style.left = iL + 'px'
oWin.style.top = iT + 'px'
// if(parseInt(oWin.style.top) < $('.container').css('top').replace('px','')) {
// oWin.style.top = $('.container').css('top')
// }
return false
}
document.onmouseup = window.onblur = oH2.onlosecapture = function () {
bDrag = false
oH2.releaseCapture && oH2.releaseCapture()
}
}
</script>
</body>
</html>
上方可以直接复制和粘贴,可以使用,用自己的key 自己去申请去
map = new AMap.Map('container', {
zoom: 10,//级别
center: [121.479921, 31.286758],//中心点坐标
viewMode: '3D',//使用3D视图
features: ['bg', 'road'],
showIndoorMap: false,
});
这个是创建地图的代码
var lnglats = [
[116.368904, 39.923423],
[116.382122, 39.921176],
[116.387271, 39.922501],
[116.398258, 39.914600]
];
var infoWindow = new AMap.InfoWindow({ offset: new AMap.Pixel(0, -30) });
for (var i = 0, marker; i < lnglats.length; i++) {
var marker = new AMap.Marker({
position: lnglats[i],
map: map,
title: i
});
marker.content = `
<div class="headall" id="heatMapBox${collectHeatMapPointIndex}">
我是第 (${i + 1})个Marker
<div id="heatMapBox${collectHeatMapPointIndex}" class = 'dd' onclick="lock(this,${i},'heatMapBox${collectHeatMapPointIndex}')"> 21212</div>
</div> `;
marker.on('click', markerClick);
marker.emit('click', { target: marker });
}
function markerClick(e) {
collectHeatMapPointIndex++
if (colllist.length == 4) {
infoWindow.setContent(e.target.content);
infoWindow.open(map, e.target.getPosition());
return
} else {
colllist.push(marker)
infoWindow.setContent(e.target.content);
infoWindow.open(map, e.target.getPosition());
}
}
这一部分是创建marker点和信息框,大家可以看到 marker.content 这个是写相关内容的,看到里面有一个点击事件 就是克隆的函数
map.setFitView();
始终显示在屏幕的范围
function lock(e, id, idClass) {
console.log("我要克隆", idClass);
colllist.forEach(item => {
if (item.getTitle() == id) {
// console.log(22);
var oldTop = $('#' + idClass).offset().top;
var oldLeft = $('#' + idClass).offset().left;
if ($('#' + idClass).hasClass("active")) {
console.log("eww");
return
}
$('#container').prepend($('#' + idClass).clone())
item.setLabel({
offset: new AMap.Pixel(0, 0), //设置文本标注偏移量
content: '',
direction: 'top'
});
$('#' + idClass).css('top', oldTop + 'px')
$('#' + idClass).css('left', oldLeft + 'px')
$('#' + idClass).addClass("active")
drag(idClass, 'headall')
}
})
}
这一点很重要,就是克隆的函数 $('#container').prepend($('#' + idClass).clone()) 这个是地图的id div的
function drag(fatherBox, sonBox) {
var oWin = document.getElementById(`${fatherBox}`)
var oH2 = document.querySelector("." + sonBox)
var bDrag = false
var disX = (disY = 0)
oH2.onmousedown = function (event) {
var event = event || window.event
bDrag = true
disX = event.clientX - oWin.offsetLeft
disY = event.clientY - oWin.offsetTop
this.setCapture && this.setCapture()
return false
}
document.onmousemove = function (event) {
if (!bDrag) return
var event = event || window.event
var iL = event.clientX - disX
var iT = event.clientY - disY
var maxL = document.documentElement.clientWidth - oWin.offsetWidth
var maxT = document.documentElement.clientHeight - oWin.offsetHeight
iL = iL < 0 ? 0 : iL
iL = iL > maxL ? maxL : iL
iT = iT < 0 ? 0 : iT
iT = iT > maxT ? maxT : iT
oWin.style.marginTop = oWin.style.marginLeft = 0
oWin.style.left = iL + 'px'
oWin.style.top = iT + 'px'
// if(parseInt(oWin.style.top) < $('.container').css('top').replace('px','')) {
// oWin.style.top = $('.container').css('top')
// }
return false
}
document.onmouseup = window.onblur = oH2.onlosecapture = function () {
bDrag = false
oH2.releaseCapture && oH2.releaseCapture()
}
}
大家可以仔细的看这个是拖拽的函数,可以研究一下
#container {
width: 500px;
height: 500px;
}
.headall {
z-index: 111111;
position: absolute;
background: red;
}
.amap-info-content {
height: 200px;
width: 200px;
}
.active {
display: block;
}
以上是相关的样式 主要是
.headall {
z-index: 111111;
position: absolute;
background: red;
}
一个定位要注意点,还有这个层级!!!
以上就是全部的代码和写的逻辑啦!,不懂的请及时评论,这个是一个简单的例子