网络编程 
首页 > 网络编程 > 浏览文章

百度地图自定义控件分享

(编辑:jimmy 日期: 2025/12/27 浏览:3 次 )

废话不多说了,直接奉上代码;

复制代码 代码如下:
<script>
        var map = new BMap.Map('allmap');
        var Bcenter = new BMap.Point(116.404,39.915);
        map.centerAndZoom(Bcenter,11);
        //自定义控件
        function staticControl(){
            this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;
            this.defaultOffset = new BMap.Size(10,10);
        }
        //继承Control的API
        staticControl.prototype = new BMap.Control();
        //初始化控件
        staticControl.prototype.initialize=function(map){
            var div = document.createElement('div');
            var e1 = document.createElement('input');
            e1.type = 'button';
            e1.value = '重置';
            e1.onclick=function(){
                statics();
            }
            div.appendChild(e1);
            var e2=document.createElement('input');
            e2.type = 'button';
            e2.value = '缩小';
            e2.onclick=function(){
                endStatics();
            }
            div.appendChild(e2);
            var e3 = document.createElement("input");
            e3.type = "button";
            e3.value = "放大";
            e3.onclick = function () {
               setStatics();
            }
            div.appendChild(e3);
            //添加DOM元素到地图中
            map.getContainer().appendChild(div);
            //返回DOM
            return div;
        }
        //创建控件实例
        var staticsCtrl = new staticControl();
        //添加到地图当中
        map.addControl(staticsCtrl);
        function statics(){
            map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
        }
        function endStatics(){
            map.setZoom(map.getZoom()-2);
        }
        function setStatics(){
            map.setZoom(map.getZoom()+2)
        }
    </script>

上面代码就是个人正在使用的百度地图的自定义控件,希望大家能够喜欢。

上一篇:深入理解JavaScript系列(40):设计模式之组合模式详解
下一篇:jQuery实现仿淘宝带有指示条的图片转动切换效果完整实例
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。