Table of Contents
Event Listening
Event parameters and this
Home Web Front-end HTML Tutorial Baidu map event processing--getting the latitude and longitude (simple use of Baidu map)_html/css_WEB-ITnose

Baidu map event processing--getting the latitude and longitude (simple use of Baidu map)_html/css_WEB-ITnose

Jun 24, 2016 am 11:39 AM

Map events overview

JavaScript in the browser is "event driven", which means that JavaScript responds to interactions by generating events and expects the program to "listen" for interested Activity. For example, in a browser, the user's mouse and keyboard interactions can create events that propagate within the DOM. Programs that are interested in certain events register JavaScript event listeners for those events and execute code when they receive those events.

Baidu Map API has its own event model. Programmers can listen to custom events of map API objects. The usage method is similar to DOM events. But please note that Map API events are independent and different from standard DOM events.

Event Listening

Most objects in the Baidu Map API contain the addEventListener method, through which you can listen to object events. For example, BMap.Map contains click, dblclick and other events. These events will be triggered under specific circumstances, and the listening function will get the corresponding event parameter e. For example, when the user clicks on the map, the e parameter will contain the geographical location point corresponding to the mouse.

For events on Map API objects, please refer to the complete API reference documentation.

The addEventListener method has two parameters: the name of the event to be listened to and the function to be called when the event is triggered. In the example below, whenever the user clicks on the map, an alert box will pop up.

var map = new BMap.Map("container");    map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);    map.addEventListener("click", function(){     alert("您点击了地图。");    });
Copy after login

By listening to events, you can also capture the status of after the event is triggered. The following example shows the latitude and longitude information of the center of the map after the user drags the map.

var map = new BMap.Map("container");    map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);    map.addEventListener("dragend", function(){     var center = map.getCenter();     alert("地图中心点变更为:" + center.lng + ", " + center.lat);    });
Copy after login

Event parameters and this

In the standard DOM event model (DOM Level 2 Events) , the listening function will get an event object e, in which information about the event can be obtained. At the same time, in the listening function, this will point to the DOM element that triggered the event. The event model of Baidu Map API is similar to this. The event object e is passed in the event listening function. Each e parameter contains at least the event type (type) and the object that triggered the event (target). The API also ensures that this within the function points to the API object that triggered (and was also bound to) the event.

For example, get the latitude and longitude coordinates of the click through the parameter e.

var map = new BMap.Map("container");    map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);    map.addEventListener("click", function(e){     alert(e.point.lng + ", " + e.point.lat);    });
Copy after login

Or get the zoomed level of the map through this.

var map = new BMap.Map("container");    map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);    map.addEventListener("zoomend", function(){     alert("地图缩放至:" + this.getZoom() + "级");    });
Copy after login

Remove listening events

When you no longer want to listen for events, you can Event listener is removed. Each API object provides removeEventListener to remove event listening functions.

In the example below, the user’s first click on the map will trigger the event listening function. The event listening function is removed inside the function, so subsequent click operations will not trigger the listening function.

var map = new BMap.Map("container");    map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);    function showInfo(e){     alert(e.point.lng + ", " + e.point.lat);     map.removeEventListener("click", showInfo);    }    map.addEventListener("click", showInfo);
Copy after login

Example:

Click the latitude and longitude where the map pops up

<html><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />    <style type="text/css">        body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";font-family:"微软雅黑";}        #allmap{width:100%;height:500px;}        p{margin-left:5px; font-size:14px;}    </style>    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>    <title>地图单击事件</title></head><body>    <div id="allmap"></div>    <p>添加点击地图监听事件,点击地图后显示当前经纬度</p></body></html><script type="text/javascript">    // 百度地图API功能    var map = new BMap.Map("allmap");    map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);    function showInfo(e){        alert(e.point.lng + ", " + e.point.lat);    }    map.addEventListener("click", showInfo);</script>
Copy after login

Thank you for reading!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles