<!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>
百度地图自定义覆盖物
最新推荐文章于 2022-12-12 17:45:29 发布