Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
640 views
in Technique[技术] by (71.8m points)

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

图片.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());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

mapObj.remove(markers);
markers = []

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...