原文:【百度地图API】如何给自定义覆盖物添加事件

摘要:

  给marker、lable、circle等Overlay添加事件很简单,直接addEventListener即可。那么,自定义覆盖物的事件应该如何添加呢?我们一起来看一看~

-----------------------------------------------------------------------------------------

一、定义构造函数并继承Overlay

// 定义自定义覆盖物的构造函数  
function SquareOverlay(center, length, color){
this._center = center;
this._length = length;
this._color = color;
}
// 继承API的BMap.Overlay
SquareOverlay.prototype = new BMap.Overlay();

二、初始化自定义覆盖物

// 实现初始化方法  
SquareOverlay.prototype.initialize = function(map){
// 保存map对象实例
this._map = map;
// 创建div元素,作为自定义覆盖物的容器
var div = document.createElement("div");
div.style.position = "absolute";
// 可以根据参数设置元素外观
div.style.width = this._length + "px";
div.style.height = this._length + "px";
div.style.background = this._color;
// 将div添加到覆盖物容器中
map.getPanes().markerPane.appendChild(div);
// 保存div实例
this._div = div;
// 需要将div元素作为方法的返回值,当调用该覆盖物的show、
// hide方法,或者对覆盖物进行移除时,API都将操作此元素。
return div;
}

三、绘制覆盖物

// 实现绘制方法  
SquareOverlay.prototype.draw = function(){
// 根据地理坐标转换为像素坐标,并设置给容器
var position = this._map.pointToOverlayPixel(this._center);
this._div.style.left = position.x - this._length / 2 + "px";
this._div.style.top = position.y - this._length / 2 + "px";
}

四、添加覆盖物

//添加自定义覆盖物  
var mySquare = new SquareOverlay(map.getCenter(), 100, "red");
map.addOverlay(mySquare);

五、给自定义覆盖物添加事件

1、显示事件

SquareOverlay.prototype.show = function(){  
if (this._div){
this._div.style.display = "";
}
}

添加完以上显示覆盖物事件后,只需要下面这句话,就可以显示覆盖物了。

mySquare.show();

2、隐藏覆盖物

// 实现隐藏方法  
SquareOverlay.prototype.hide = function(){
if (this._div){
this._div.style.display = "none";
}
}

添加完以上code,只需使用这句话,即可隐藏覆盖物。

mySquare.hide();

3、改变覆盖物颜色

SquareOverlay.prototype.yellow = function(){  
if (this._div){
this._div.style.background = "yellow";
}
}

上面这句话,是把覆盖物的背景颜色改成黄色,使用以下语句即可生效:

mySquare.yellow();

“第五部分、给覆盖物添加事件”小结:

我们在地图上添加了一个红色覆盖物,然后分别添加“显示、隐藏、改变颜色”的事件。示意图如下:

那么,我们需要在html里,先写出map的容器,和3个按钮。

<div style="width:520px;height:340px;border:1px solid gray" id="container"></div>
<p>
<input type="button" value="移除覆盖物" onclick="mySquare.hide();" />
<input type="button" value="显示覆盖物" onclick="mySquare.show();" />
<input type="button" value="变成黄色" onclick="mySquare.yellow();" />
</p>

然后,在javascript中,添加这三个函数:

// 实现显示方法  
SquareOverlay.prototype.show = function(){
if (this._div){
this._div.style.display = "";
}
}
// 实现隐藏方法
SquareOverlay.prototype.hide = function(){
if (this._div){
this._div.style.display = "none";
}
}

//改变颜色的方法
SquareOverlay.prototype.yellow = function(){
if (this._div){
this._div.style.background = "yellow";
}
}

六、如何给自定义覆盖物添加点击事件(这章重要!很多人问的)

比如,我们给自定义覆盖物点击click事件。首先,需要添加一个addEventListener 的事件。如下:

SquareOverlay.prototype.addEventListener = function(event,fun){
this._div['on'+event] = fun;
}

再写该函数里面的参数,比如click。这样就跟百度地图API里面的覆盖物事件一样了。

mySquare.addEventListener('click',function(){
alert('click');
});

同理,添加完毕addEventListener之后,还可以添加其他鼠标事件,比如mouseover。

mySquare.addEventListener('mousemover',function(){
alert('鼠标移上来了');
});

七、全部源代码

自定义覆盖物

 <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>自定义覆盖物的点击事件</title>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2"></script>
</head>
<body>
<div style="width:520px;height:340px;border:1px solid gray" id="container"></div>
<p>
<input type="button" value="移除覆盖物" onclick="mySquare.hide();" />
<input type="button" value="显示覆盖物" onclick="mySquare.show();" />
<input type="button" value="变成黄色" onclick="mySquare.yellow();" />
</p>
</body>
</html>
<script type="text/javascript">
var map = new BMap.Map("container"); // 创建Map实例
var point = new BMap.Point(116.404, 39.915); // 创建点坐标
map.centerAndZoom(point,15); // 初始化地图,设置中心点坐标和地图级别。

//1、定义构造函数并继承Overlay
// 定义自定义覆盖物的构造函数
function SquareOverlay(center, length, color){
this._center = center;
this._length = length;
this._color = color;
}
// 继承API的BMap.Overlay
SquareOverlay.prototype = new BMap.Overlay();

//2、初始化自定义覆盖物
// 实现初始化方法
SquareOverlay.prototype.initialize = function(map){
// 保存map对象实例
this._map = map;
// 创建div元素,作为自定义覆盖物的容器
var div = document.createElement("div");
div.style.position = "absolute";
// 可以根据参数设置元素外观
div.style.width = this._length + "px";
div.style.height = this._length + "px";
div.style.background = this._color;
// 将div添加到覆盖物容器中
map.getPanes().markerPane.appendChild(div);
// 保存div实例
this._div = div;
// 需要将div元素作为方法的返回值,当调用该覆盖物的show、
// hide方法,或者对覆盖物进行移除时,API都将操作此元素。
return div;
}

//3、绘制覆盖物
// 实现绘制方法
SquareOverlay.prototype.draw = function(){
// 根据地理坐标转换为像素坐标,并设置给容器
var position = this._map.pointToOverlayPixel(this._center);
this._div.style.left = position.x - this._length / 2 + "px";
this._div.style.top = position.y - this._length / 2 + "px";
}

//4、显示和隐藏覆盖物
// 实现显示方法
SquareOverlay.prototype.show = function(){
if (this._div){
this._div.style.display = "";
}
}
// 实现隐藏方法
SquareOverlay.prototype.hide = function(){
if (this._div){
this._div.style.display = "none";
}
}

//5、添加其他覆盖物方法
//比如,改变颜色
SquareOverlay.prototype.yellow = function(){
if (this._div){
this._div.style.background = "yellow";
}
}

//6、自定义覆盖物添加事件方法
SquareOverlay.prototype.addEventListener = function(event,fun){
this._div['on'+event] = fun;
}

//7、添加自定义覆盖物
var mySquare = new SquareOverlay(map.getCenter(), 100, "red");
map.addOverlay(mySquare);

//8、 为自定义覆盖物添加点击事件
mySquare.addEventListener('click',function(){
alert('click');
});
</script>

八、感谢大家支持!

API常见问题总结贴:http://tieba.baidu.com/p/1147019448

【百度地图API】如何给自定义覆盖物添加事件的更多相关文章

  1. 百度地图API如何给自定义覆盖物添加事件

    摘要: 给marker.lable.circle等Overlay添加事件很简单,直接addEventListener即可.那么,自定义覆盖物的事件应该如何添加呢?我们一起来看一看~ --------- ...

  2. 【百度地图API】如何自定义地图图层?实例:制作麻点图(自定义图层+热区)

    原文:[百度地图API]如何自定义地图图层?实例:制作麻点图(自定义图层+热区) 摘要:自定义地图图层的用途十分广泛.常见的应用,比如制作魔兽地图和清华校园地图(使用切图工具即可轻松实现).今天我们来 ...

  3. 百度地图api窗口信息自定义

    百度地图加载完后,完全可以用dom方法操作,比较常用的就是点击mark的弹窗,利用jQuery可以很快的创建弹窗,需要注意的就是地图都是异步加载,所以绑定时间要用 jQuery 事件 - delega ...

  4. 百度地图api之如何自定义标注图标

    在百度地图api中,默认的地图图标是一个红色的椭圆形.但是在项目中常常要求我们建立自己的图标,类似于我的这个 操作很简单,分如下几步进行 步骤一:先ps一个图标,大小要合适,如果要背景透明的,记得保存 ...

  5. 百度地图API 海量点 自定义添加信息

    <!--添加百度地图--> <script type="text/javascript" src="http://api.map.baidu.com/a ...

  6. 百度地图API的事件处理:覆盖物的如何阻止冒泡

    百度地图,为了让事件使用的更方便,进行一层封装 详情可以看官方的文档 http://developer.baidu.com/map/jsdevelop-5.htm 主要的修改点: 1. 使用事件代理. ...

  7. 百度地图API示例:使用vue添加删除覆盖物

    1.index.html <script type="text/javascript" src="http://api.map.baidu.com/api?v=2. ...

  8. 百度地图API示例之小实践 添加代理商标注

    地图坐标无非是经度纬度. 每个代理商都有他的经度纬度参数,就能够在地图上标注出来了. 效果如下: 功能包括 标记代理商 显示导航 显示距离 测量距离 点击选中等 其中测距用到的是自定义控件 地图根据城 ...

  9. 百度地图API 循环向 marker 添加 click事件

    使用百度地图API,循环向marker添加InfoWindow时,所有的marker点击弹出的inforwindow为最后一个添加的infowindow,百度后,使用js闭包解决此问题,直接贴代码: ...

随机推荐

  1. JDK5什么是新的堵塞队列线程(四)

    一. 堵塞队列与普通队列: 队列是一种主要的数据类型,其典型特征是先进先出. 堵塞队列和普通队列的差别在于: 当队列为空时.从队列中获取元素的线程会被堵塞.直到其它的线程往空的队列里插入新的元素: 当 ...

  2. c++日历v1.12版

    ////////////////////////////新增信息修改功能,未完善. #include<iostream> #include <string> #include& ...

  3. CI-持续集成(2)-软件工业“流水线”技术实现(转)

    1   概述 持续集成(Continuous Integration)是一种软件开发实践.在本系列文章的前一章节已经对其背景及理论体系进行了介绍.本小节则承接前面提出的理论构想进行具体的技术实现. & ...

  4. shell脚本—根据文件个数定时备份

    最近在了解Jenkins时,出现这样一个问题:linux ext3系统目录下的子目录个数不能超过31998个(参考:http://www.xshell.net/linux/1267.html),但je ...

  5. 软测试是一个烂摊子?NO——【软测试】

    软测试是一个烂摊子权?我开始也是这么认为的.这充分证明,.我并没有考虑,整个合并没有类似的项目. 前几天跟慕夏交流了怎样做总结,听完她讲的,我開始学着为细节的知识点找联系. 只是今天跟老师一交流,才发 ...

  6. RH253读书笔记(1)-Lab 1 System Monitoring

    Lab 1 System Monitoring Goal: To build skills to better assess system resources, performance and sec ...

  7. 使用CASE表达式替代SQL Server中的动态SQL

    原文:使用CASE表达式替代SQL Server中的动态SQL 翻译自: http://www.mssqltips.com/sqlservertip/1455/using-the-case-expre ...

  8. ehcache历史变迁及常用API的使用(转)

    ehcache是一个用Java实现的使用简单,高速,实现线程安全的缓存管理类库,ehcache提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案.同时ehcache作为开放源 ...

  9. 如何获得 oracle RAC 11g asm spfile S档

     方法一: [root@vmrac1 ~]# su - grid [grid@vmrac1 ~]$ sqlplus / as sysasm SQL*Plus: Release 11.2.0.3.0 ...

  10. CSS截取字符串,额外的文本显示以省略号

    最近的诀窍css还有这么叼炸天的侧~~~ 善待似论坛页面,头像70px,但username不限啊,English中国能够.你说如果他采取"我的名字是这么长啊.你该怎么办呢".那老和 ...