search
HomeWeb Front-endJS TutorialHow to use JS and Baidu Maps to implement the popular city display function on the map

How to use JS and Baidu Maps to implement the popular city display function on the map

How to use JS and Baidu Maps to implement the map's popular city display function

In modern society, maps have become an indispensable part of people's lives. With the development of technology, more and more websites and applications are beginning to use various map services to provide a better user experience. Baidu Maps is one of the most commonly used map services in China. It provides a wealth of functions and interfaces, allowing developers to flexibly use maps to display various information.

This article will introduce how to use JS and Baidu Maps to implement the map's popular city display function. We will use Baidu Maps API to obtain the latitude and longitude data of the city, and use JS to display the data on the map. At the same time, we will also use Baidu Map's markers, information windows and other functions to achieve the effect of displaying detailed information such as city name, popularity and so on when clicking on a city.

First of all, we need to introduce the JS file and CSS file of Baidu Map into the HTML file. The code is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>地图热门城市展示</title>
    <link rel="stylesheet" type="text/css" href="http://api.map.baidu.com/library/MarkerTool/1.2/src/MarkerTool.css">
</head>
<body>
    <div id="map" style="width: 100%; height: 500px;"></div>
    <script src="http://api.map.baidu.com/api?v=2.0&ak=你的API密钥"></script>
    <script src="http://api.map.baidu.com/library/MarkerTool/1.2/src/MarkerTool.js"></script>
</body>
</html>

In this example, we use the MarkerTool library of Baidu Maps to achieve a more convenient marking function. It should be noted that when introducing the JS file of the map, please replace "your API key" with your own Baidu Map API key.

Next, we need to write JS code to implement specific functions. First, we need to initialize the map, the code is as follows:

// 初始化地图
var map = new BMap.Map("map");
var point = new BMap.Point(116.404, 39.915);
map.centerAndZoom(point, 5);

In this example, we first create a map object and specify the initial map center point and zoom level. We use the BMap.Point class to represent a geographical coordinate point.

Next, we need to get the latitude and longitude data of the city and display it on the map. Assume that we already have a data array cityData containing city names and popularity. The code is as follows:

var cityData = [
    {name: "北京", point: "116.403119,39.915861", hot: 100},
    {name: "上海", point: "121.487899,31.249162", hot: 90},
    {name: "广州", point: "113.307649,23.120049", hot: 80},
    // ...
];

// 显示城市标记
for (var i = 0; i < cityData.length; i++) {
    var cityName = cityData[i].name;
    var point = cityData[i].point.split(",");
    var hot = cityData[i].hot;

    var marker = new BMap.Marker(new BMap.Point(point[0], point[1]));
    map.addOverlay(marker);

    marker.addEventListener("click", function() {
        var infoWindow = new BMap.InfoWindow(cityName + ",热度:" + hot);
        this.openInfoWindow(infoWindow);
    });
}

In this example, we use a for loop to traverse the data array cityData and add cities one by one on the map. mark. For each city marker, we use the BMap.Marker class to create a marker object and specify the location information of the marker. We then add the marker to the map using the map.addOverlay() method.

At the same time, we added a click event listener for each mark. When the mark is clicked, an information window will pop up to display the name and popularity information of the city.

Through the above code, we can display popular cities on the map and implement the function of popping up detailed information when clicking on a city.

To sum up, this article introduces how to use JS and Baidu Maps to implement the popular city display function on the map, and gives specific code examples. Through this function, we can display the distribution of cities more intuitively and provide users with richer geographical information.

The above is the detailed content of How to use JS and Baidu Maps to implement the popular city display function on the map. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

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