百度地图标注添加数字序号示例(Javascript API)

给标注(Marker)添加数字序号会使地图更直观,我想到了两种方法可以给百度地图标注添加数字,代码分享出来,大家可自行选择。

使用带数字的图标替换标注默认图标

var icon = new BMap.Icon("/img/02.png", new BMap.Size(50, 44));
var marker = new BMap.Marker(map.getCenter(), {icon:icon});
map.addOverlay(marker);



在标注上添加文本标注Label

//创建并返回带数字的Marker
var createMarker = function(point, content){
    var marker = new BMap.Marker(point);
    var label = new BMap.Label(content,{offset:new BMap.Size(((content+"").length==1)?5:0,3)});
    label.setStyle({background:'none', color:'#fff', border:'none'});
    marker.setLabel(label);
    return marker;
}
map.addOverlay(createMarker(map.getCenter(), 2));



完整示例

<!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" />
<title>Hello, World</title>
<style type="text/css">
html{height:100%}
body{height:100%;margin:0px;padding:0px}
#container{height:100%}
</style>
<script type="text/javascript" src="//api.map.baidu.com/api?v=3.0&ak=您的密钥">
</script>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">
    //创建并返回带数字的Marker
    var createMarker = function(point, content){
        var marker = new BMap.Marker(point);
        var label = new BMap.Label(content,{offset:new BMap.Size(((content+"").length==1)?5:0,3)});
        label.setStyle({background:'none', color:'#fff', border:'none'});
        marker.setLabel(label);
        return marker;
    }

    var map = new BMap.Map("container");
    var point = new BMap.Point(120.124469,30.26139);
    map.centerAndZoom(point, 15);
    map.enableScrollWheelZoom(true);
    //在地图上添加Marker
    for(var i=1;i<=15;i++){
        var point = new BMap.Point(map.getCenter().lng+0.001*i, map.getCenter().lat);
        map.addOverlay(createMarker(point, i));
    }
</script>
</body>
</html>



评论: 0 | 引用: 0 | 查看次数: 4737
发表评论
登录后再发表评论!