Home > Article > Web Front-end > 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!