百度地图自定义覆盖物

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=XXXXX"></script>
    <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
    <title>给多个点添加信息窗口</title>
</head>
<body>
<div id="allmap"></div>

</body>
</html>

<script type="text/javascript">
    // 百度地图API功能
    var mp = new BMap.Map("allmap",{enableMapClick:false});
    mp.centerAndZoom(new BMap.Point(116.3964,39.9093), 15);
    mp.enableScrollWheelZoom();
    // 复杂的自定义覆盖物
    function ComplexCustomOverlay(point, cityName,total,cityId){
        this._point = point;
        this._cityName = cityName;
        this._total = total;
        this._cityId = cityId;
    }
    ComplexCustomOverlay.prototype = new BMap.Overlay();
    ComplexCustomOverlay.prototype.initialize = function(map){
        this._map = map;
        var div = this._div = document.createElement("div");
        div.style.position = "absolute";
        div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
        div.style.backgroundColor = "#EE5D5B";
        div.style.color = "white";
        div.style.height = "100px";
        div.style.width = "100px";
        div.style.padding = "10px";
        div.style.lineHeight = "100px";
        div.style.whiteSpace = "normal";
        div.style.MozUserSelect = "none";
        div.style.fontSize = "12px";
        div.style.borderRadius = "50px";
        div.style.textAlign="center";
        div.setAttribute("id", this._cityId);

        var h5 = this._h5 = document.createElement("h5");
        div.appendChild(h5);
        h5.style.marginTop = "15px";
        h5.appendChild(document.createTextNode(this._cityName));
        var span = this._span = document.createElement("h6");
        div.appendChild(span);
        span.appendChild(document.createTextNode('共' + this._total +'家仓储'));
        var that = this;

        var arrow = this._arrow = document.createElement("div");

        arrow.style.position = "absolute";
        arrow.style.width = "11px";
        arrow.style.height = "10px";
        arrow.style.top = "22px";
        arrow.style.left = "10px";
        arrow.style.overflow = "hidden";
        div.appendChild(arrow);

        div.onmouseover = function(){
            this.style.backgroundColor = "green";
            this.style.borderColor = "#0000ff";
            //arrow.style.backgroundPosition = "0px -20px";
        }

        div.onmouseout = function(){
            this.style.backgroundColor = "#EE5D5B";
            this.style.borderColor = "#BC3B3A";
            //arrow.style.backgroundPosition = "0px 0px";
        }
        div.onclick =function(){
            attribute(this.getAttribute('id'));
        }
        mp.getPanes().labelPane.appendChild(div);

        return div;
    }
    ComplexCustomOverlay.prototype.draw = function(){
        var map = this._map;
        var pixel = map.pointToOverlayPixel(this._point);
        this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
        this._div.style.top  = pixel.y - 30 + "px";
    }

    var data_info = [[116.417854,39.921988,"广元",10,1],
        [116.406605,39.921585,"成都",100,2],
        [116.412222,39.912345,"阿坝藏族羌族自治州",1000,3]
    ];
    for(var i=0;i<data_info.length;i++){
        var myCompOverlay = new ComplexCustomOverlay(new BMap.Point(data_info[i][0],data_info[i][1]),data_info[i][2],data_info[i][3],data_info[i][4]);  // 创建标注
        mp.addOverlay(myCompOverlay);               // 将标注添加到地图中
    }
    //获取覆盖物位置
    function attribute(id){
        window.location.href='http://www.baidu.com?id=' + id;
    }
</script>





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值