实现效果:
图标用的是字体图标,为了方便动态改变颜色
关键代码
function ComplexCustomOverlay(params) {
let defaultParams = {
point: { lng: 0, lat: 0 },
fontSize: 28,
deg: 0,
className: '',
color: 'red',
};
defaultParams = Object.assign(defaultParams, params);
this._point = defaultParams.point;
this._fontSize = defaultParams.fontSize;
this._deg = defaultParams.deg;
this._className = defaultParams.className;
this._color = defaultParams.color;
}
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.height = this._fontSize + 'px';
div.style.width = this._fontSize + 'px';
var arrow = (this._arrow = document.createElement('i'));
arrow.className = 'iconfont ' + this._className;
arrow.style.display = 'inline-block';
arrow.style.fontSize = this._fontSize + 'px';
arrow.style.color = this._color;
arrow.style.transform = 'rotate(' + this._deg + 'deg)';
div.appendChild(arrow);
map.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._fontSize) / 2 + 'px';
this._div.style.top = pixel.y - parseInt(this._fontSize) / 2 + 'px';
};
ComplexCustomOverlay.prototype.addEventListener = function (event, fun) {
this._div['on' + event] = fun;
};
调用方式:
let params = {
point: new BMap.Point(114.00491526048702, 22.53049351287813),
deg: 90,
className: 'icon-shangzhijiantou1',
color: 'red',
};
var myCompOverlay = new ComplexCustomOverlay(params);
map.addOverlay(myCompOverlay);
|