This article briefly introduces the application of Baidu Maps. Here I introduce a function that is to add an event method on a layer you set. Please refer to it if necessary.
It is very simple to add events to overlays such as marker, label, circle, etc., just addEventListener directly. So, how to add events for custom overlays? Let’s take a look~
------------------------------------------------ -------------------------------------------------one , define the constructor and inherit 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. Change the color of the overlay
代码如下 | 复制代码 |
SquareOverlay.prototype.yellow = function(){ <br> if (this._div){ <br> this._div.style.background = "yellow"; <br> } <br>} |
mySquare.yellow();Summary of "Part 5, Adding Events to the Overlay": We added a red overlay on the map. Then add "show, hide, change color" events respectively. The schematic diagram is as follows:

代码如下 | 复制代码 |
<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> |
代码如下 | 复制代码 |
// 实现显示方法 |
6. How to add a click event to a custom overlay (This chapter is important! Many people ask) For example, we add a click event to a custom overlay. First, you need to add an addEventListener event. As follows:
代码如下 | 复制代码 |
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 |

7月14日消息,百度昨日宣布开源其旗下的MQTTBroker消息中间件BifroMQ,该消息中间件是由Java实现,具有高性能和分布式的特点。据称,BifroMQ是百度智能云物联网核心套件IoTCore的基础技术之一。BifroMQ采用了Serverless架构,并且无缝集成了原生的多租户支持。该消息中间件源自百度物联网团队多年的技术积累,旨在支持构建大规模的物联网设备连接和消息系统。据小编了解,BifroMQ采用了负载独立子集群设计,可以高效处理连接会话、消息转发和消息存储等工作负载。各子集群

PHP实现对接百度智能分类接口的简单指南近年来,随着人工智能的快速发展,智能分类技术被广泛应用于许多领域。百度智能分类接口就是其中一种应用,它可以根据传入的文本数据对其进行分类,帮助我们更好地理解文本的内容。本文将介绍如何使用PHP语言对接百度智能分类接口,并给出相应的代码示例。一、准备工作注册百度开发者账号并登录。创建一个应用并获取应用的APIKey和S

PHP实现对接百度语音识别接口的步骤与注意事项前言:随着人工智能技术的快速发展,语音识别技术也逐渐成熟并被广泛应用。百度语音识别接口是一款强大的云端语音识别服务,提供丰富的功能和灵活的配置选项,方便开发者根据自己的需求进行定制化开发。本文将利用PHP语言来实现对接百度语音识别接口的操作步骤,并对注意事项进行详细介绍。步骤一:申请百度语音识别接口首先,我们需要

据本站8月9日16时许报道,百度旗下产品出现了大规模故障,例如百度网盘在使用过程中出现了视频播放失败的情况,其他业务也有用户反映无法打开或崩溃的现象。经过本站的调查,发现是由于百度CDN服务异常所致百度网盘回应称,对此问题十分抱歉并正在紧急修复中,请耐心等待,感谢您的理解五分钟后,百度网盘回复说问题已解决,请您尝试重新登录,感谢您的支持CDN(内容分发网络)通过将源站内容分发至全球各地的加速节点,使用户在请求网站资源时能够被调度至距离最近的加速节点,从而直接获得所需内容,提高用户对网站资源的访问

PHP如何对接百度文心一言接口?百度文心一言接口是一个提供随机返回一句文心话的接口,使用起来非常简单。本文将介绍PHP如何对接百度文心一言接口,并提供代码示例。首先,我们需要申请百度文心一言接口的访问权限。在百度开放平台上创建一个应用,并获取到相应的APIKey。接下来,我们可以使用PHP中的cURL库来发送HTTP请求并获取接口返回的数据。下面是一个简单

Python与百度AI接口对接的基础入门指南引言:随着人工智能技术的快速发展,百度AI的接口提供了许多强大的功能和服务。Python作为一种功能强大且易于学习的编程语言,与百度AI的接口对接特别方便。本文将介绍一些常见的百度AI接口,并提供相应的Python代码示例,帮助读者快速入门。一、百度语音识别接口:百度语音识别接口可以用于将语音转换为文本,实现语音识

Python与百度智能语音接口对接的实践经验与技巧分享一、引言百度智能语音接口是一种强大的语音识别技术,可以将语音转化为对应的文字,并且支持多种场景下的语音输入,如麦克风输入、文件输入等。在实际开发中,与百度智能语音接口进行对接可以帮助我们实现语音识别、语音转写等功能。本文将分享一些Python与百度智能语音接口对接的实践经验与技巧,并提供代码示例供参考。二

使用Python实现百度图像识别API对接教程一、介绍随着人工智能的发展,图像识别技术已广泛应用于各个领域。而百度图像识别API是一款强大而易用的图像识别工具,可以帮助开发者快速实现图像分类、物体检测、图像搜索等功能。本文将详细介绍如何使用Python语言对接百度图像识别API,并给出代码示例。二、准备工作注册百度云账号首先,你需要在百度云官网注册一个账号,


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool