百度地图 添加标注

给地图增加一个事件监听,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>26.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);

    map.addEventListener("click", function (e) {
        point = new BMap.Point(e.point.lng, e.point.lat);
        var marker = new BMap.Marker(point);
        map.addOverlay(marker);
    }); 
</script>

 

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

导航