qq.maps.Overlay 类

继承自MVCObject

自定义覆盖物,通过继承此类,可以自定义Dom元素的样式及事件。

构造函数

构造函数
Overlay()

方法

方法 返回值 说明
construct() none 实现这个接口来初始化自定义的Dom元素,此方法会在setMap(map)后被调用,panes和projection属性也将被初始化。
draw() none 实现这个接口来绘制和更新自定义的dom元素。
destroy() none 实现这个接口来删除自定义的Dom元素,此方法会在setMap(null)后被调用。
getMap() Map 返回覆盖物容器所在的map对象。
getPanes() MapPanes 返回地图覆盖物容器列表。
getProjection() MapCanvasProjection 返回覆盖物容器的相对像素坐标或是经纬度坐标。
setMap(map:Map) none 设置覆盖物容器所在的map对象。

实例

本示例中,介绍如何通过继承Overlay创建简单的自定义覆盖物

JavaScript
  1. var map = new qq.maps.Map(document.getElementById("container"));
  2.  
  3. function CustomOverlay(position, index) {
  4. this.index = index;
  5. this.position = position;
  6. }
  7. CustomOverlay.prototype = new qq.maps.Overlay();
  8. //定义construct,实现这个接口来初始化自定义的Dom元素
  9. CustomOverlay.prototype.construct = function() {
  10. var div = this.div = document.createElement("div");
  11. var divStyle = this.div.style;
  12. divStyle.position = "absolute";
  13. divStyle.width = "24px";
  14. divStyle.height = "24px";
  15. divStyle.backgroundColor = "#FFFFFF";
  16. divStyle.border = "1px solid #000000";
  17. divStyle.textAlign = "center";
  18. divStyle.lineHeight = "24px";
  19. divStyle.borderRadius = "15px";
  20. divStyle.cursor = "pointer";
  21. this.div.innerHTML = this.index;
  22. //将dom添加到覆盖物层
  23. var panes = this.getPanes();
  24. //设置panes的层级,overlayMouseTarget可接收点击事件
  25. panes.overlayMouseTarget.appendChild(div);
  26.  
  27. var self = this;
  28. this.div.onclick = function() {
  29. alert(self.index);
  30. }
  31. }
  32. //实现draw接口来绘制和更新自定义的dom元素
  33. CustomOverlay.prototype.draw = function() {
  34. var overlayProjection = this.getProjection();
  35. //返回覆盖物容器的相对像素坐标
  36. var pixel = overlayProjection.fromLatLngToDivPixel(this.position);
  37. var divStyle = this.div.style;
  38. divStyle.left = pixel.x - 12 + "px";
  39. divStyle.top = pixel.y - 12 + "px";
  40. }
  41. //实现destroy接口来删除自定义的Dom元素,此方法会在setMap(null)后被调用
  42. CustomOverlay.prototype.destroy = function() {
  43. this.div.onclick = null;
  44. this.div.parentNode.removeChild(this.div);
  45. this.div = null
  46. }
  47. var latlng = map.getCenter();
  48. var overlay = new CustomOverlay(latlng, 0);
  49. overlay.setMap(map);
JavaScript+HTML
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset = utf-8" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  7. <title>腾讯地图</title>
  8. <style>
  9. html,
  10. body {
  11. height: 100%;
  12. margin: 0px;
  13. padding: 0px
  14. }
  15. #container {
  16. width: 100%;
  17. height: 100%
  18. }
  19. </style>
  20. <script src="https://map.qq.com/api/js?v=2.exp&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77"></script>
  21. </head>
  22.  
  23. <body>
  24. <div id="container" style="width:603px;height:300px"></div>
  25. <script>
  26. var map = new qq.maps.Map(document.getElementById("container"));
  27.  
  28. function CustomOverlay(position, index) {
  29. this.index = index;
  30. this.position = position;
  31. }
  32. CustomOverlay.prototype = new qq.maps.Overlay();
  33. //定义construct,实现这个接口来初始化自定义的Dom元素
  34. CustomOverlay.prototype.construct = function() {
  35. var div = this.div = document.createElement("div");
  36. var divStyle = this.div.style;
  37. divStyle.position = "absolute";
  38. divStyle.width = "24px";
  39. divStyle.height = "24px";
  40. divStyle.backgroundColor = "#FFFFFF";
  41. divStyle.border = "1px solid #000000";
  42. divStyle.textAlign = "center";
  43. divStyle.lineHeight = "24px";
  44. divStyle.borderRadius = "15px";
  45. divStyle.cursor = "pointer";
  46. this.div.innerHTML = this.index;
  47. //将dom添加到覆盖物层
  48. var panes = this.getPanes();
  49. //设置panes的层级,overlayMouseTarget可接收点击事件
  50. panes.overlayMouseTarget.appendChild(div);
  51.  
  52. var self = this;
  53. this.div.onclick = function() {
  54. alert(self.index);
  55. }
  56. }
  57. //实现draw接口来绘制和更新自定义的dom元素
  58. CustomOverlay.prototype.draw = function() {
  59. var overlayProjection = this.getProjection();
  60. //返回覆盖物容器的相对像素坐标
  61. var pixel = overlayProjection.fromLatLngToDivPixel(this.position);
  62. var divStyle = this.div.style;
  63. divStyle.left = pixel.x - 12 + "px";
  64. divStyle.top = pixel.y - 12 + "px";
  65. }
  66. //实现destroy接口来删除自定义的Dom元素,此方法会在setMap(null)后被调用
  67. CustomOverlay.prototype.destroy = function() {
  68. this.div.onclick = null;
  69. this.div.parentNode.removeChild(this.div);
  70. this.div = null
  71. }
  72. var latlng = map.getCenter();
  73. var overlay = new CustomOverlay(latlng, 0);
  74. overlay.setMap(map);
  75. </script>
  76. </body>
  77.  
  78. </html>