代码
var map = new BMap.Map('map');
var center = new BMap.Point(116.407845, 39.914101);
map.centerAndZoom(center, 14);
map.enableScrollWheelZoom();
// 自定义覆盖物
function MyCustomOverlay(point, text, mouseoverText) {
this._point = point;
this._text = text;
this._overText = mouseoverText;
}
MyCustomOverlay.prototype = new BMap.Overlay();
MyCustomOverlay.prototype.initialize = function (map) {
this._map = map;
var div = (this._div = document.createElement('div'));
div.className = 'house-overlay';
div.style.position = 'absolute';
div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
var span = (this._span = document.createElement('span'));
span.className = 'name';
span.appendChild(document.createTextNode(this._text));
div.appendChild(span);
var badge = (this._badge = document.createElement('span'));
badge.className = 'badge badge-hover';
badge.appendChild(document.createTextNode(this._overText));
div.appendChild(badge);
var arrow = (this._arrow = document.createElement('div'));
arrow.className = 'left-arrow';
div.appendChild(arrow);
map.getPanes().labelPane.appendChild(div);
div.onmouseover = function () {
// badge.classList.add("badge-hover");
};
div.onmouseout = function () {
// badge.classList.remove("badge-hover");
};
return div;
};
MyCustomOverlay.prototype.draw = function () {
var map = this._map;
var pixel = map.pointToOverlayPixel(this._point);
this._div.style.left = pixel.x - 10 + 'px';
this._div.style.top = pixel.y - 30 + 'px';
};
var areas = [
{point: new BMap.Point(116.384361, 40.072387), name: '银湖海岸城', count: 856},
{point: new BMap.Point(116.368407, 39.921419), name: '西单金融街', count: 1203},
{point: new BMap.Point(116.424077, 39.903676), name: '崇文新世界', count: 547},
{point: new BMap.Point(116.384951, 39.900861), name: '宣武椿树园', count: 389},
{point: new BMap.Point(116.440194, 39.93046), name: '东四朝阳门', count: 724},
{point: new BMap.Point(116.415187, 39.92461), name: '王府井东华', count: 968},
];
for (var i = 0; i < areas.length; i++) {
var item = areas[i];
var overlay = new MyCustomOverlay(item.point, item.name, item.count + ' 套');
map.addOverlay(overlay);
}
预览