


Use JavaScript and Tencent Maps to implement the query function of surrounding facilities on the map
Using JavaScript and Tencent Maps to realize the query function of surrounding facilities on the map
In recent years, with the rapid development of the Internet and mobile devices, map services have become more important in people’s lives. Sex is increasing day by day. Whether it is travel navigation, surrounding facility inquiries or location sharing, map services play an indispensable role. Among many map services, Tencent Maps is loved by the majority of users for its precise positioning and rich functions. This article will introduce how to use JavaScript and Tencent Maps to implement the map surrounding facility query function, and provide corresponding code examples.
1. Preparatory work
Before implementing the query function of surrounding facilities on the map, we need to do some preparatory work first. First, we need to register a developer account on the Tencent Maps open platform and create an application. The registration address is: https://lbs.qq.com/. After the registration is completed, we can obtain a developer key (key), which is used for authentication when calling the Tencent Map API.
2. Building the HTML page
When building the HTML page, we need to introduce the relevant code of the Tencent Map JavaScript API. Add the following code to the HTML page:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>地图周边设施查询</title> </head> <body> <div id="map" style="width: 100%; height: 400px;"></div> <script src="https://map.qq.com/api/js?v=2.exp&key=开发者密钥"></script> <script src="script.js"></script> </body> </html>
Among them, Developer Key
needs to be replaced with the actual key you obtained on the Tencent Map Open Platform.
3. Writing JavaScript code
In the JavaScript code, we will implement the map initialization, positioning and surrounding facility query functions by calling the Tencent Map API. Create a script.js
file and copy the following code into it:
// 创建地图实例 var map = new qq.maps.Map(document.getElementById("map"), { zoom: 13, // 设置地图缩放级别 }); // 获取用户当前位置 navigator.geolocation.getCurrentPosition(function(position) { var lat = position.coords.latitude; // 获取纬度 var lng = position.coords.longitude; // 获取经度 // 将地图中心点设置为用户当前位置 map.setCenter(new qq.maps.LatLng(lat, lng)); // 在地图上添加当前位置的标记 new qq.maps.Marker({ position: new qq.maps.LatLng(lat, lng), map: map, }); }); // 在地图上进行周边设施查询 function searchNearby(keyword) { var service = new qq.maps.SearchService({ map: map, }); // 根据关键词进行周边查询 service.searchNearBy(keyword, map.getCenter(), 1000); } // 为搜索按钮绑定事件 document.getElementById("search-btn").addEventListener("click", function() { var keyword = document.getElementById("search-input").value; searchNearby(keyword); });
In this code, a map instance is first created and displayed on the page map
in the container. Then, get the user's current location by calling the navigator.geolocation.getCurrentPosition()
method, and position the map center point at the user's location. Then, a marker is added to the map marking the user's current location.
Finally, a searchNearby()
function is defined, which receives a keyword as a parameter and calls searchNearBy of
qq.maps.SearchService ()
method to query surrounding facilities. The query range is the area 1 km near the center of the current map.
In order to trigger the map query, we added a search button and bound a click event to it through the addEventListener
method, which will call searchNearby()
function to perform query operations. At the same time, we set up an input box for the search button to enter search keywords.
4. Effect Demonstration
After completing the above code writing, we refresh the page and see a map based on Tencent Maps displayed on the page. Moreover, the center point of the map will be automatically positioned to the current user's location. At this time, we can enter keywords (such as restaurants, banks, schools, etc.) in the input box, and then click the search button to see nearby facility markers with corresponding keywords on the map.
Summary
This article introduces how to use JavaScript and Tencent Maps to implement the map surrounding facility query function. By calling the Tencent Map API, we can easily display maps on the page and provide users with convenient map services by obtaining the user's current location and searching for surrounding facilities. I hope this article is helpful to you, and everyone is welcome to try it and apply it to your own projects.
The above is the detailed content of Use JavaScript and Tencent Maps to implement the query function of surrounding facilities on the map. For more information, please follow other related articles on the PHP Chinese website!

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use
