百度地图API-给自定义覆盖物添加事件方法
本文章简单的介绍了一下关于百度地图的应用,这里我介绍一个功能就是在自己定的层上给加个事件方法,有需要的参考一下。
给marker、lable、circle等Overlay添加事件很简单,直接addEventListener即可。那么,自定义覆盖物的事件应该如何添加呢?我们一起来看一看~
-----------------------------------------------------------------------------------------一、定义构造函数并继承Overlay代码如下 | 复制代码 |
// 定义自定义覆盖物的构造函数 function SquareOverlay(center, length, color){ this._center = center; this._length = length; this._color = color; } // 继承API的BMap.Overlay |
代码如下 | 复制代码 |
// 实现初始化方法 <br>SquareOverlay.prototype.initialize = function(map){ <br>// 保存map对象实例 <br> this._map = map; <br> // 创建div元素,作为自定义覆盖物的容器 <br> var div = document.createElement("div"); <br> div.style.position = "absolute"; <br> // 可以根据参数设置元素外观 <br> div.style.width = this._length + "px"; <br> div.style.height = this._length + "px"; <br> div.style.background = this._color; <br> // 将div添加到覆盖物容器中 <br> map.getPanes().markerPane.appendChild(div); <br> // 保存div实例 <br> this._div = div; <br> // 需要将div元素作为方法的返回值,当调用该覆盖物的show、 <br> // hide方法,或者对覆盖物进行移除时,API都将操作此元素。 <br> return div; <br>} |
代码如下 | 复制代码 |
// 实现绘制方法 <br>SquareOverlay.prototype.draw = function(){ <br>// 根据地理坐标转换为像素坐标,并设置给容器 <br> var position = this._map.pointToOverlayPixel(this._center);<br> this._div.style.left = position.x - this._length / 2 + "px"; <br> this._div.style.top = position.y - this._length / 2 + "px"; <br>} |
代码如下 | 复制代码 |
//添加自定义覆盖物 <br>var mySquare = new SquareOverlay(map.getCenter(), 100, "red"); <br>map.addOverlay(mySquare); |
代码如下 | 复制代码 |
SquareOverlay.prototype.show = function(){ <br> if (this._div){ <br> this._div.style.display = ""; <br> } <br>} |
代码如下 | 复制代码 |
mySquare.show(); |
// 实现隐藏方法 <br>
代码如下 | 复制代码 |
SquareOverlay.prototype.hide = function(){ if (this._div){ this._div.style.display = "none"; } } |
mySquare.hide();3、改变覆盖物颜色
代码如下 | 复制代码 |
SquareOverlay.prototype.yellow = function(){ <br> if (this._div){ <br> this._div.style.background = "yellow"; <br> } <br>} |
mySquare.yellow();“第五部分、给覆盖物添加事件”小结:我们在地图上添加了一个红色覆盖物,然后分别添加“显示、隐藏、改变颜色”的事件。示意图如下:那么,我们需要在html里,先写出map的容器,和3个按钮。
代码如下 | 复制代码 |
<div style="width:520px;height:340px;border:1px solid gray" id="container"></div><br><p><br> <input type="button" value="移除覆盖物" onclick="mySquare.hide();"><br> <input type="button" value="显示覆盖物" onclick="mySquare.show();"><br> <input type="button" value="变成黄色" onclick="mySquare.yellow();"><br></p> |
代码如下 | 复制代码 |
// 实现显示方法 |
六、如何给自定义覆盖物添加点击事件(这章重要!很多人问的)比如,我们给自定义覆盖物点击click事件。首先,需要添加一个addEventListener 的事件。如下:
代码如下 | 复制代码 |
SquareOverlay.prototype.addEventListener = function(event,fun){<br> this._div['on'+event] = fun;<br>} |
代码如下 | 复制代码 |
mySquare.addEventListener('click',function(){<br> alert('click');<br>}); |
代码如下 | 复制代码 |
mySquare.addEventListener('mousemover',function(){<br> alert('鼠标移上来了');<br>}); |
代码如下 | 复制代码 |
1 2 3 4 5 6 7 8 9 10
15 16 17 |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1
Easy-to-use and free code editor
