原文:【百度地图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('鼠标移上来了');});

七、全部源代码

自定义覆盖物

 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>自定义覆盖物的点击事件</title> 6 <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2"></script> 7 </head> 8 <body> 9 <div style="width:520px;height:340px;border:1px solid gray" id="container"></div>10 <p>11     <input type="button" value="移除覆盖物" onclick="mySquare.hide();" />12     <input type="button" value="显示覆盖物" onclick="mySquare.show();" />13     <input type="button" value="变成黄色" onclick="mySquare.yellow();" />14 </p>15 </body>16 </html>17 <script type="text/javascript">18 var map = new BMap.Map("container");            // 创建Map实例19 var point = new BMap.Point(116.404, 39.915);    // 创建点坐标20 map.centerAndZoom(point,15);                     // 初始化地图,设置中心点坐标和地图级别。21 22 //1、定义构造函数并继承Overlay23 // 定义自定义覆盖物的构造函数  24 function SquareOverlay(center, length, color){  25  this._center = center;  26  this._length = length;  27  this._color = color;  28 }  29 // 继承API的BMap.Overlay  30 SquareOverlay.prototype = new BMap.Overlay(); 31 32 //2、初始化自定义覆盖物33 // 实现初始化方法  34 SquareOverlay.prototype.initialize = function(map){  35 // 保存map对象实例  36  this._map = map;      37  // 创建div元素,作为自定义覆盖物的容器  38  var div = document.createElement("div");  39  div.style.position = "absolute";      40  // 可以根据参数设置元素外观  41  div.style.width = this._length + "px";  42  div.style.height = this._length + "px";  43  div.style.background = this._color;    44  // 将div添加到覆盖物容器中  45  map.getPanes().markerPane.appendChild(div);    46  // 保存div实例  47  this._div = div;    48  // 需要将div元素作为方法的返回值,当调用该覆盖物的show、  49  // hide方法,或者对覆盖物进行移除时,API都将操作此元素。  50  return div;  51 }52 53 //3、绘制覆盖物54 // 实现绘制方法  55 SquareOverlay.prototype.draw = function(){  56 // 根据地理坐标转换为像素坐标,并设置给容器 57  var position = this._map.pointToOverlayPixel(this._center);58  this._div.style.left = position.x - this._length / 2 + "px";  59  this._div.style.top = position.y - this._length / 2 + "px";  60 }61 62 //4、显示和隐藏覆盖物63 // 实现显示方法  64 SquareOverlay.prototype.show = function(){  65  if (this._div){  66    this._div.style.display = "";  67  }  68 }    69 // 实现隐藏方法  70 SquareOverlay.prototype.hide = function(){  71  if (this._div){  72    this._div.style.display = "none";  73  }  74 }75 76 //5、添加其他覆盖物方法77 //比如,改变颜色 78 SquareOverlay.prototype.yellow = function(){  79  if (this._div){  80     this._div.style.background = "yellow"; 81  }     82 }83 84 //6、自定义覆盖物添加事件方法85 SquareOverlay.prototype.addEventListener = function(event,fun){86     this._div['on'+event] = fun;87 }88 89 //7、添加自定义覆盖物  90 var mySquare = new SquareOverlay(map.getCenter(), 100, "red");  91 map.addOverlay(mySquare);92 93 //8、 为自定义覆盖物添加点击事件94 mySquare.addEventListener('click',function(){95     alert('click');96 });97 </script>

八、感谢大家支持!

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

【百度地图API】如何给自定义覆盖物添加事件相关推荐

  1. html百度地图标记图标,百度地图api之换自定义标注图标

    百度地图API自定义地图 html,body{margin:0;padding:0;} .iw_poi_title {color:#CC5522;font-size:14px;font-weight: ...

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

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

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

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

  4. 百度地图POI周边检索/自定义覆盖物

    鉴于百度地图不计其数的poi兴趣点 很多app在开发的时候都会选择调用百度地图资源 //主界面Activity package com.zhuang.search;import java.util.A ...

  5. 百度地图 Api v3.0 自定义信息窗体样式

    一.效果图 二.代码 注意要先引入: <script src="http://api.map.baidu.com/api?v=3.0&ak=IN43cyju8PVLGfSNwl ...

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

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

  7. 百度地图API删除指定的覆盖物Marker

    部分思路代码: 1.给地图map添加覆盖物Marker,注意给marker设定一个唯一表示,我这里用的是后端传过来的id const point = new BMap.Point(item.lng, ...

  8. 百度地图API和高德地图API资料集锦

    [高德地图API]从零开始学高德JS API(五)路线规划--驾车|公交|步行 [高德地图API]从零开始学高德JS API(四)搜索服务--POI搜索|自动完成|输入提示|行政区域|交叉路口|自有数 ...

  9. 数据视化Echarts+百度地图API实现市县区级下钻

    开始 这两天公司有个页面需要做数据可视化的展示,数据视化采用的是Echarts+百度地图API做展示,需要用到县级区级下钻的一个联动效果发现网上关于Echarts做到县区级下钻的资料很少,有的话也不是 ...

最新文章

  1. 科大星云诗社动态20210211
  2. websocke 在线测试地址
  3. PowerDesigner生成sql语句时自动导出注释
  4. 2152:聪聪可可(点分治)
  5. Linux内核 设备树操作常用API【转】
  6. 物联网卡得持续增长对企业带来怎样的挑战
  7. bagging boosting 随机森林 GBDT对比
  8. 骑士CMS人才招聘系统初次接触
  9. 图片去水印的原理_神奇的Photoshop去除图片水印方法
  10. 2020数学建模国赛A题 炉温曲线 心得
  11. 激光雷达:点云语义分割算法
  12. 盛大无传奇 啥时离职成见面招呼语
  13. 程序设计入门C语言 --- 素数和
  14. IBM小型机发展史1980-2007
  15. ML:模型训练/模型评估中常用的两种方法代码实现(留一法一次性切分训练和K折交叉验证训练)
  16. mysql导入数据时 USING BTREE 错误解决办法
  17. java 微信转账 ca_error_java,微信支付退款_微信支付退款接口调用证书出现错误,java,微信支付退款,ssl - phpStudy...
  18. 如何使用charles+mock替换接口返回来测试
  19. Python Turtle:小海龟创意绘画,仰望星空,脚踏实地,配有背景音乐哦!(附源码,可以学习如何添加背景音乐,如何使用渐变色)
  20. Android适配手机与平板屏幕尺寸

热门文章

  1. TS DataType
  2. arm-linux-ld segment fault,segment fault 定位 与 远程 gdb
  3. 实时获取ccd图像_图像处理基础
  4. C语言-数据结构-可变长顺序表的查找操作
  5. 轻松学c语言编程.pdf等,轻松学编程 轻松学C语言编程pdf
  6. c++刷题(18/100)树
  7. Android 实现手写板技术
  8. geolocation/ 百度地图api Geolocation 定位当前城市信息
  9. MFCButton Memory leak(内存泄露问题)
  10. Android 调用系统相机拍照,生命周期重走OnCreate,导致无数据的解决办法