百度地图 删除标注

如何实现:

方法一、给地图增加一个事件监听,addEventListener,当右击地图时,出现右键菜单,可删除标注。

方法二、给标注增加事件监听,右键点击标注,即可删除

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.4"></script>
    <script type="text/javascript" src="http://api.map.baidu.com/library/TextIconOverlay/1.2/src/TextIconOverlay_min.js"></script>
    <script type="text/javascript" src="http://api.map.baidu.com/library/MarkerClusterer/1.2/src/MarkerClusterer_min.js"></script>
    <script type="text/javascript" src="http://api.map.baidu.com/library/LuShu/1.2/src/LuShu_min.js"></script>
    <title>27.1删除标注</title>
</head>
<body>
    <div class="wrapper">
        <div class="container clearfix">
            <div class="clearfix">
                <div style="clear: both;">
                    <div style="float: left; width: 500px; height: 340px; border: 1px solid gray" id="container">
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    var map = new BMap.Map("container");
    var point = new BMap.Point(116.404, 39.950507);
    map.centerAndZoom(point, 16);

    var marker = new BMap.Marker(point);
    map.addOverlay(marker);

    marker.addEventListener("rightclick", function () {
        map.removeOverlay(marker);
    });

    var menu = new BMap.ContextMenu();
    var txtMenuItem = [
    {
        text: "删除标注",
        callback: function () {
            map.removeOverlay(marker);
        }
    }];

    for (var i = 0; i < txtMenuItem.length; i++) {
        menu.addItem(new BMap.MenuItem(txtMenuItem[i].text, txtMenuItem[i].callback, 100));
    }
    map.addContextMenu(menu);
 
</script>

 

posted on 2014-12-04 17:02  AlexGeng  阅读(11299)  评论(0编辑  收藏  举报

导航