百度地图api接口仿租房平台多覆盖物实例

html部分

<!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=你的ak;"></script>
	//执行不了 先换成你自己的ak 
	<script type="text/javascript" src="http://mat1.gtimg.com/libs/jquery/1.12.0/jquery.js"></script>
	<style type="text/css">
     {
      overflow:hidden; 
      text-overflow:ellipsis;
      white-space:nowrap;

    }
  </style>
	<title>demo</title>
</head>
<body>
	<div id="allmap"></div>
</body>

</html>


js部分

// 百度地图API功能
var mp = new BMap.Map("allmap");
mp.centerAndZoom(new BMap.Point(125.62,42.49), 15); //初始原点坐标
mp.enableScrollWheelZoom();
twoData = [
	{'x':'125.728',
	 'y': '42.54',
	 'name':'梅小河1',
	 'num':'35',
	},
	{'x':'125.712',
	 'y': '42.49',
	 'name':'梅小河2',
	 'num':'74',
	},
	{'x':'125.709',
	 'y': '42.51',
	 'name':'梅小河3',
	 'num':'74',
	},
];
threeData = [
	{'x':'125.77 ','y': '42.92','id':'118773'},
	{'x':'125.86 ','y': '42.74','id':'118774'},
	{'x':'125.69 ','y': '42.54','id':'118775'},
	{'x':'125.72 ','y': '42.54','id':'118776'},
	{'x':'127.05 ','y': '45.41','id':'118777'},
	{'x':'125.72 ','y': '42.54','id':'118778'},
	{'x':'125.68 ','y': '42.54','id':'118779'},
	{'x':'125.45 ','y': '42.38','id':'118780'},
	{'x':'125.43 ','y': '42.36','id':'118781'},
	{'x':'127.05 ','y': '45.41','id':'118782'},
	{'x':'125.68 ','y': '42.52','id':'118783'},
	{'x':'125.72 ','y': '42.55','id':'118784'},
	{'x':'125.62 ','y': '42.52','id':'118785'},
	{'x':'127.05 ','y': '45.41','id':'118786'},
	{'x':'125.71 ','y': '42.56','id':'118787'},
	{'x':'125.70 ','y': '42.54','id':'118788'},
	{'x':'125.69 ','y': '42.54','id':'118789'},
	{'x':'125.71 ','y': '42.55','id':'118790'},
	{'x':'125.71 ','y': '42.56','id':'118791'},
	{'x':'125.72 ','y': '42.54','id':'118792'},
	{'x':'125.71 ','y': '42.55','id':'118793'},
	{'x':'125.71 ','y': '42.55','id':'118794'},
	{'x':'125.69 ','y': '42.56','id':'118795'},
	{'x':'124.82 ','y': '45.12','id':'118796'},
	{'x':'124.82 ','y': '45.12','id':'118797'},
	{'x':'125.71 ','y': '42.53','id':'118798'},
	{'x':'125.72 ','y': '42.54','id':'118799'},
	{'x':'125.70 ','y': '42.55','id':'118800'},
	{'x':'124.82 ','y': '45.12','id':'118801'},
	{'x':'125.70 ','y': '42.54','id':'118802'},
	{'x':'125.68 ','y': '42.52','id':'118803'},
	{'x':'120.30 ','y': '30.02','id':'118804'},
	{'x':'124.82 ','y': '45.12','id':'118805'},
	{'x':'125.71 ','y': '42.53','id':'118806'},
	{'x':'119.79 ','y': '33.23','id':'118807'},
	{'x':'124.82 ','y': '45.12','id':'118808'},
	{'x':'125.70 ','y': '42.53','id':'118809'},
	{'x':'125.69 ','y': '42.53','id':'118810'},
	{'x':'124.30 ','y': '42.91','id':'118811'},
	{'x':'125.64 ','y': '42.44','id':'118812'},
	{'x':'116.64 ','y': '39.91','id':'118813'},
	{'x':'125.64 ','y': '42.40','id':'118814'},
	{'x':'126.05 ','y': '42.69','id':'118815'},
	{'x':'125.69 ','y': '42.56','id':'118816'},
	{'x':'126.05 ','y': '42.69','id':'118817'},
	{'x':'124.82 ','y': '45.12','id':'118818'},
	{'x':'124.82 ','y': '45.12','id':'118819'},
	{'x':'124.82 ','y': '45.12','id':'118820'},
	{'x':'124.82 ','y': '45.12','id':'118821'},
	{'x':'125.43 ','y': '42.38','id':'118822'},
	{'x':'125.77 ','y': '42.92','id':'118823'},
	{'x':'125.71 ','y': '42.53','id':'118824'},
	{'x':'125.72 ','y': '42.54','id':'118825'},
	{'x':'125.72 ','y': '42.54','id':'118826'},
	{'x':'127.05 ','y': '45.41','id':'118827'},
	{'x':'125.71 ','y': '42.55','id':'118828'},
	{'x':'125.77 ','y': '42.92','id':'118829'},
	{'x':'125.91 ','y': '42.73','id':'118830'},
	{'x':'125.41 ','y': '42.32','id':'118831'},
	{'x':'125.72 ','y': '42.54','id':'118832'},
	{'x':'125.71 ','y': '42.45','id':'118833'},
	{'x':'124.82 ','y': '45.12','id':'118834'},
	{'x':'124.82 ','y': '45.12','id':'118835'},
	{'x':'125.78 ','y': '42.65','id':'118836'},
	{'x':'125.70 ','y': '42.53','id':'118837'},
	{'x':'125.61 ','y': '42.42','id':'118838'},
	{'x':'125.67 ','y': '42.49','id':'118839'},
	{'x':'125.72 ','y': '42.55','id':'118840'},
	{'x':'117.77 ','y': '39.89','id':'118841'},
	{'x':'125.71 ','y': '42.55','id':'118842'},
	{'x':'125.70 ','y': '42.53','id':'118843'},
	{'x':'125.71 ','y': '42.55','id':'118844'},
	{'x':'125.70 ','y': '42.55','id':'118845'},
	{'x':'105.68 ','y': '37.49','id':'118846'},
	{'x':'125.71 ','y': '42.54','id':'118847'},
	{'x':'125.72 ','y': '42.54','id':'118848'},
	{'x':'124.82 ','y': '45.12','id':'118849'},
	{'x':'125.72 ','y': '42.54','id':'118850'},
	{'x':'125.88 ','y': '42.62','id':'118851'},
	{'x':'125.64 ','y': '42.44','id':'118852'},
	{'x':'125.72 ','y': '42.54','id':'118853'},
	{'x':'125.68 ','y': '42.52','id':'118854'},
	{'x':'125.86 ','y': '42.65','id':'118855'},
	{'x':'124.82 ','y': '45.12','id':'118856'},
	{'x':'124.82 ','y': '45.12','id':'118857'},
	{'x':'125.64 ','y': '42.44','id':'118858'},
	{'x':'109.88 ','y': '22.74','id':'118859'},
	{'x':'111.11 ','y': '39.04','id':'118860'},
	{'x':'125.75 ','y': '42.61','id':'118861'},
	{'x':'125.69 ','y': '42.53','id':'118862'},
	{'x':'125.70 ','y': '42.54','id':'118863'},
	{'x':'125.48 ','y': '42.37','id':'118864'},
	{'x':'125.50 ','y': '42.26','id':'118865'},
	{'x':'125.64 ','y': '42.44','id':'118866'},
	{'x':'125.64 ','y': '42.44','id':'118867'},
	{'x':'125.64 ','y': '42.44','id':'118868'},
	{'x':'114.49 ','y': '36.61','id':'118869'},
	{'x':'125.72 ','y': '42.55','id':'118870'},
	{'x':'125.70 ','y': '42.55','id':'118871'},
	{'x':'125.70 ','y': '42.53','id':'118872'},
	{'x':'125.70 ','y': '42.53','id':'118873'},
	{'x':'125.45 ','y': '42.38','id':'118874'},
	{'x':'125.71 ','y': '42.45','id':'118875'},
	{'x':'125.71 ','y': '42.55','id':'118876'},
	{'x':'127.05 ','y': '45.41','id':'118877'},
	{'x':'125.76 ','y': '42.56','id':'118878'},
	{'x':'125.71 ','y': '42.55','id':'118879'},
	{'x':'127.05 ','y': '45.41','id':'118880'},
	{'x':'125.72 ','y': '42.52','id':'118881'},
	{'x':'125.69 ','y': '42.54','id':'118882'},
	{'x':'125.72 ','y': '42.54','id':'118883'},

	
];





// 定义 1级  大模块 
function oneLevel(point, text, num){
  console.log(point);
  this._point = point;
  this._text = text;
  this._num = num;
}
oneLevel.prototype = new BMap.Overlay();
oneLevel.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 = "#15baf3";
  div.style.color = "white";
  div.style.width = "100px";
  div.style.height = "100px";
  div.style.borderRadius= '50%';
  div.style.padding = "2px";
  div.style.lineHeight = "18px";
  div.style.whiteSpace = "nowrap";
  div.style.MozUserSelect = "none";
  div.style.fontSize = "15px";
  div.style.textAlign = "center";
  
  $(div).attr('id','sss');
  var span = this._span = document.createElement("div");
  var span_1 = this._span = document.createElement("div");
  span.title = this._text;
  span.style.margin= '24px 0px 0px 7px';
  span.style.width = '80px';
  span.style.overflow = 'hidden';
  span.style.textOverflow = 'ellipsis';
  span.style.whiteSpace = 'nowrap';
  
  span.style.height = "35px"
  span_1.style.height = "35px"
  div.appendChild(span);
  div.appendChild(span_1);
  span.appendChild(document.createTextNode(this._text));      
  span_1.appendChild(document.createTextNode(this._num));
  var that = this;
  mp.getPanes().labelPane.appendChild(div);
  return div;
}
oneLevel.prototype.draw = function(){
  var map = this._map;
  var pixel = map.pointToOverlayPixel(this._point);
  this._div.style.left = pixel.x -50  + "px";
  this._div.style.top  = pixel.y -50 + "px";
}


var point = new BMap.Point(125.62,42.49);
var one = new oneLevel(point, "梅河口市梅河口市梅河口市梅河口市梅河口市梅河口市","123");
mp.addOverlay(one);

var marker = new BMap.Marker(point);  // 创建标注
mp.addOverlay(marker);              // 将标注添加到地图中
mp.centerAndZoom(point, 15);
var opts = {
  width : 200,     // 信息窗口宽度
  height: 400,     // 信息窗口高度
  title : "海底捞王府井店" , // 信息窗口标题
  enableMessage:true,//设置允许信息窗发送短息
  message:"亲耐滴,晚上一起吃个饭吧?戳下面的链接看下地址喔~"
}
var infoWindow = new BMap.InfoWindow('<div>地址:北京市东城区王府井大街88号乐天银泰百货八层<div><div>地址:北京市东城区王府井大街88号乐天银泰百货八层<div><div><a style="float:right" >XX</a><div>', opts);  // 创建信息窗口对象 
// one.addEventListener("click", function(){          
// });
$('#sss').click(function(){
  mp.openInfoWindow(infoWindow,point); //开启信息窗口
})


function twoLevel(point, text, num){
  this._point = point;
  this._text = text;
  this._num = num;
}
twoLevel.prototype = new BMap.Overlay();
twoLevel.prototype.initialize = function(map){
  this._map = map;
  var div = this._div = document.createElement("div");
  div.className = 'twodiv';
  div.style.position = "absolute";
  div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
  div.style.backgroundColor = "#15baf3";
  div.style.color = "white";
  div.style.width = "100px";
  div.style.height = "100px";
  div.style.borderRadius= '50%';
  div.style.padding = "2px";
  div.style.lineHeight = "18px";
  div.style.whiteSpace = "nowrap";
  div.style.MozUserSelect = "none";
  div.style.fontSize = "15px";
  div.style.textAlign = "center";
  div.style.lineHeight = "70px";

  var span = this._span = document.createElement("div");
  var span_1 = this._span = document.createElement("div");
  span.style.height = "35px"
  span_1.style.height = "35px"
  div.appendChild(span);
  div.appendChild(span_1);
  span_1.appendChild(document.createTextNode(this._num));
  span.appendChild(document.createTextNode(this._text));      
  var that = this;
  mp.getPanes().labelPane.appendChild(div);
  return div;
}
twoLevel.prototype.draw = function(){
  var map = this._map;
  var pixel = map.pointToOverlayPixel(this._point);
  this._div.style.left = pixel.x  + "px";
  this._div.style.top  = pixel.y  + "px";
}



function threeLevel(point,id){
  this._point = point;
  this._id = id;
}
threeLevel.prototype = new BMap.Overlay();
threeLevel.prototype.initialize = function(map){
  this._map = map;
  var div = this._div = document.createElement("div");
  $(div).attr('id',this._id);
  div.className = 'threediv';
  div.style.position = "absolute";
  div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
  div.style.backgroundColor = "#15baf3";
  div.style.color = "white";
  div.style.width = "30px";
  div.style.height = "30px";
  div.style.borderRadius= '50%';
  div.style.padding = "2px";
  div.style.lineHeight = "18px";
  div.style.whiteSpace = "nowrap";
  div.style.MozUserSelect = "none";
  div.style.fontSize = "15px";
  div.style.textAlign = "center";
  div.style.lineHeight = "70px";
  $(div).attr('onclick','ddd(this)')
  var that = this;

  mp.getPanes().labelPane.appendChild(div);
  return div;
}


threeLevel.prototype.draw = function(){
  var map = this._map;
  var pixel = map.pointToOverlayPixel(this._point);
  this._div.style.left = pixel.x  + "px";
  this._div.style.top  = pixel.y  + "px";
}
  

$.each(twoData,function(i,n){
	var two = new twoLevel(new BMap.Point(n.x,n.y), n.name,n.num);
	mp.addOverlay(two);
}) 

$('.twodiv').hide() 

$.each(threeData,function(i,n){
	var three = new threeLevel(new BMap.Point(n.x,n.y),n.id);
	mp.addOverlay(three);

})
$('.threediv').hide() 

mp.addEventListener("zoomend", function(evt){
	zoom =  mp.getZoom();
	console.log(zoom)
	if(zoom <= 12){
		one.show();
		$('.twodiv').hide()
	}else if(zoom >= 13 && zoom <= 15 ){
		one.hide();
		$('.twodiv').show();
		$('.threediv').hide();

	}else{
		one.hide();
		$('.twodiv').hide();
		$('.threediv').show();
	}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值