高德地图 点击地图添加marker,第一次点击的时候会出现marker图标,第二次不会

发布于 2022-09-12 02:33:50 字数 2162 浏览 38 评论 0

图片.png
高德地图 点击地图添加marker,第一次点击的时候会出现marker图标,第二次不会,这是什么情况,一下是代码

mapObj = new AMap.Map('personMapContainer', {
        resizeEnable: true,
        zoom: 12
    });
    mapObj.on('click', showAddMarker);
    
function showAddMarker(e) {
    mapObj.remove(markers);
    markers = []
    // 点标记显示内容,HTML要素字符串
    let title = new Date().getTime().toString();
    var markerContent = '' +
        '<div class="custom-content-marker">' +
        '   <img src="//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png">' +
        '   <div class="close-btn" onclick="clearMarker()">X</div>' +
        '</div>';
    var marker = new AMap.Marker({
        icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
        position: [e.lnglat.getLng(), e.lnglat.getLat()],
        // 将 html 传给 content
        content: markerContent,
        title: title,
        offset: new AMap.Pixel(-13, -30)
    });
    let lnglat = e.lnglat.getLng()+","+e.lnglat.getLat()
    marker.setMap(mapObj);
    markers.push(marker);
    // infowidnow 的 innerHTML
    var infoWindowContent =
        '<div className="custom-infowindow input-card">' +
         '<div class="infoWindow-item">' +
            // 为 infowindow 添加自定义事件
            '<input id="lnglat2container" type="button" class="infoWindowBtn" value="设为起点" onclick="setPositionType(\'' + lnglat + '\',\'1\')"/>' +
            '<input id="lnglat2container" type="button" class="infoWindowBtn" value="设为途经点" onclick="setPositionType(\'' + lnglat + '\',\'2\')" style="margin-left:5px"/>' +
            '<input id="lnglat2container" type="button" class="infoWindowBtn" value="设为终点" onclick="setPositionType(\'' + lnglat + '\',\'3\')" style="margin-left:5px"/>' +
        '</div></div>';

    // 创建一个自定义内容的 infowindow 实例
    var infoWindow = new AMap.InfoWindow({
        offset: new AMap.Pixel(0, -35),
        content: infoWindowContent
    });
    infoWindow.on('close', clearMarker)
    infoWindow.open(mapObj, marker.getPosition());
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乙白 2022-09-19 02:33:50

我试了下, 每次点击都有 marker 啊, 你这个代码有点小错误.
下面2行写反了吧, 应该先定义 markers

mapObj.remove(markers);
markers = []
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文