Home > Web Front-end > JS Tutorial > How to use JS and Baidu Maps to implement map POI search function

How to use JS and Baidu Maps to implement map POI search function

王林
Release: 2023-11-21 17:26:41
Original
1451 people have browsed it

How to use JS and Baidu Maps to implement map POI search function

How to use JS and Baidu Maps to implement the map POI search function

With the rapid development of the mobile Internet, the map navigation function has become one of the important functions in mobile applications. . In realizing the map navigation function, POI (Points of Interest) search is an essential function. Through POI search, users can quickly locate places of interest on the map, such as restaurants, hotels, gas stations, etc.

In this article, we will introduce how to use JS and Baidu Map API to implement the map POI search function, and provide specific code examples.

First of all, we need to introduce the JS file of Baidu Map API in the HTML file, which can be achieved through the following code:

<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的百度地图AK"></script>
Copy after login

Among them, Your Baidu Map AK needs Replace it with the key (AK) you applied for on the Baidu Map Open Platform.

Next, add the map container in the HTML file:

<div id="map" style="width: 100%; height: 400px;"></div>
Copy after login

Here #map is a DIV element with specified width and height, which will be used to display the map .

Next, we need to use JavaScript code to initialize the map in the map container:

var map = new BMap.Map("map");
var point = new BMap.Point(116.404, 39.915); // 初始化地图中心点位置
map.centerAndZoom(point, 15); // 设置地图中心点和缩放级别
Copy after login

Here 116.404 and 39.915 are the longitude of the map respectively and latitude, which can be adjusted according to actual needs. 15 is the zoom level of the map. The larger the value, the smaller the map display range. You can adjust it as needed.

Now, we have completed the initialization of the map. Next, we need to implement the POI search function.

The following is a simple POI search code example:

var localSearch = new BMap.LocalSearch(map); // 创建POI搜索对象

function search(keyword) {
    localSearch.search(keyword); // 执行POI搜索
}

localSearch.setSearchCompleteCallback(function(results) {
    // 清除地图上的覆盖物
    map.clearOverlays();

    // 遍历搜索结果,添加标注
    for (var i = 0; i < results.getCurrentNumPois(); i++) {
        var poi = results.getPoi(i);
        var marker = new BMap.Marker(poi.point);
        map.addOverlay(marker);
    }
});
Copy after login

In the above code, a BMap.LocalSearch object is first created, and then search Function to perform POI search. After the search results are returned, we can obtain the searched POI results through the results parameter, then traverse the results and add labels on the map.

Through the above code, we have completed the implementation of a basic map POI search function. Users can search for POIs by entering keywords and clicking the search button, and the search results will display corresponding annotations on the map.

It should be noted that the above is just a simple example. There are more details to consider in actual applications, such as the limit on the number of search results, the style setting of search results, the click event processing of search results, etc. . You can extend and improve this example according to your actual needs.

To sum up, it is not complicated to use JS and Baidu Map API to implement the map POI search function. By properly calling the API interface and processing the search results, we can implement a simple yet powerful map navigation application. I hope this article is helpful to you, and I wish you to implement an excellent map navigation application!

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template