百度地图添加复杂的自定义覆盖物

// 复杂的自定义覆盖物
function MyFocus(point) {
    this.point = point;
    //this._text = text;
}
var m_map;
继承API的BMap.Overlay
MyFocus.prototype = new BMap.Overlay();

//实现初始化方法
MyFocus.prototype.initialize = function (map) {
    m_map = 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.className = "point";

    var divChild = document.createElement("div");
    divChild.className = "focus";
    div.appendChild(divChild);
    divChild.style.position = "absolute";

    if (this._clickFun) {
        div.onclick = this._clickFun;
    }
    if (this._mouseOverFun) {
        div.onmouseover = this._mouseOverFun;
    }
    //将div添加到覆盖物容器中
    map.getPanes().markerMouseTarget.appendChild(div);

    return div;
}
//实现绘制方法
MyFocus.prototype.draw = function () {
    var map = this._map;
    var tzoom = map.getZoom();
    //根据地理坐标转换为像素坐标,并设置容器
    var pixel = map.pointToOverlayPixel(this.point);
    this._div.style.left = pixel.x - 32 + "px";
    this._div.style.top = pixel.y - 32 + "px";
}
//信息窗口
MyFocus.prototype.openWindow = function () {
    var sContent =
    "<h4 style=’margin:0 0 5px 0;padding:0.2em 0′>天安门</h4>" +
    "<img style=’float:right;margin:4px’ id=’imgDemo’ src=’http://app.baidu.com/map/images/tiananmen.jpg’ width=’139′ height=’104′ title=’天安门’/>" +
    "<p style=’margin:0;line-height:1.5;font-size:13px;text-indent:2em’>天安门坐落在中国北京市中心,故宫的南侧,与天安门广场隔长安街相望,是清朝皇城的大门…</p>" +
    "</div>";
    var infoWindow = new BMap.InfoWindow(sContent);
    var map = this._map;
    var point = this.point;
    m_map.openInfoWindow(infoWindow, point);
}

MyFocus.prototype.addEventListener = function (event, fun) {
    if (event == 'click') {
        this._clickFun = fun;
    }
    if (event == 'mouseover') {
        this._mouseOverFun = fun;
    }
}

MyFocus.prototype.getPosition = function () {
    return this.point;
}

在html中script中添加

 var marker = new MyFocus(new BMap.Point(x, y));
                    addClickListener(id, name, marker);
                    //mouseOver(marker);
                    //  mouseOver(marker);
                    map.addOverlay(marker);
在Vue中实现复杂自定义覆盖物可以使用百度地图API中提供的Overlay类和自定义覆盖物自定义覆盖物是指在地图上添加自己的DOM元素,可以通过DOM元素来实现自己的样式和交互效果。 下面是在Vue中实现复杂自定义覆盖物的步骤: 1. 安装vue-baidu-map 在终端中运行以下命令来安装vue-baidu-map: ``` npm install vue-baidu-map --save ``` 2. 注册vue-baidu-map组件 在Vue组件中注册vue-baidu-map组件: ``` import BaiduMap from 'vue-baidu-map' export default { components: { BaiduMap } } ``` 3. 在模板中添加地图 在模板中添加vue-baidu-map标签,并设置地图的中心点和缩放级别: ``` <template> <div> <BaiduMap :center="center" :zoom="zoom"></BaiduMap> </div> </template> <script> export default { data() { return { center: {lng: 116.404, lat: 39.915}, zoom: 15 } } } </script> ``` 4. 创建自定义覆盖物 在Vue组件的mounted()生命周期函数中,创建自定义覆盖物: ``` mounted() { const map = this.$refs.baiduMap.getBMap() const BMap = window.BMap const CustomOverlay = function(point, options) { this._point = point this._options = options } CustomOverlay.prototype = new BMap.Overlay() CustomOverlay.prototype.initialize = function(map) { this._map = map const div = document.createElement('div') div.style.position = 'absolute' div.style.width = '100px' div.style.height = '100px' div.style.background = '#fff' div.style.borderRadius = '50%' div.style.boxShadow = '0 0 15px rgba(0, 0, 0, 0.3)' div.style.cursor = 'pointer' div.innerHTML = '<span>自定义覆盖物</span>' div.addEventListener('click', () => { console.log('自定义覆盖物被点击了') }) map.getPanes().labelPane.appendChild(div) this._div = div return div } CustomOverlay.prototype.draw = function() { const pixel = this._map.pointToOverlayPixel(this._point) this._div.style.left = pixel.x - 50 + 'px' this._div.style.top = pixel.y - 50 + 'px' } const point = new BMap.Point(116.404, 39.915) const myOverlay = new CustomOverlay(point) map.addOverlay(myOverlay) } ``` 在上面的代码中,我们创建了一个CustomOverlay类,它继承了BMap.Overlay类,用于创建自定义覆盖物。在CustomOverlay类的initialize方法中,我们创建了一个DIV元素,并设置了DIV元素的样式和内容。我们将DIV元素添加到地图的labelPane层中,这样它就可以显示在其他标注之上了。在CustomOverlay类的draw方法中,我们将DIV元素的位置设置为自定义覆盖物的位置。 5. 添加CSS样式 最后,我们需要添加CSS样式来定义自定义覆盖物的样式: ``` <style> .custom-overlay { position: absolute; width: 100px; height: 100px; background: #fff; border-radius: 50%; box-shadow: 0 0 15px rgba(0, 0, 0, 0.3); cursor: pointer; } </style> ``` 在上面的代码中,我们定义了".custom-overlay"类来设置自定义覆盖物的样式。 这样就可以在Vue中实现复杂自定义覆盖物了。可以根据需要修改CustomOverlay类的实现,来实现更复杂自定义覆盖物
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值