Home > Web Front-end > JS Tutorial > body text

How to use JS and Baidu Maps to implement map route planning function

PHPz
Release: 2023-11-21 15:28:48
Original
863 people have browsed it

How to use JS and Baidu Maps to implement map route planning function

How to use JS and Baidu Maps to implement map route planning function

With the development of the Internet, map navigation has become an indispensable part of our lives. Implementing the map route planning function in the web page will provide users with more convenient and accurate navigation services. This article will teach you how to use JS and Baidu Map API to implement map route planning function.

Step 1: Apply for Baidu Map API Key
Before you start, you need to apply for a Baidu Map API key. For specific application steps, please refer to the official documentation of Baidu Map Open Platform. After successful application, you will get a key, which will be used to access Baidu Maps services.

Step 2: Introduce Baidu Map API
Next, introduce Baidu Map’s JS library into your HTML file. You can introduce the officially provided library file through the following code:

<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=your_api_key"></script>
Copy after login

Note that "your_api_key" in the code is replaced with the Baidu Map API key you obtained in the first step.

Step 3: Create the map
In the HTML file, you need to add a container for displaying the map. You can use the <div> element to create a container:

<div id="map"></div>
Copy after login

Then, write the code to create the map in the JS file, the code is as follows:

// 获取地图容器元素
var mapContainer = document.getElementById("map");

// 创建地图实例
var map = new BMap.Map(mapContainer);

// 设置地图中心点和缩放级别
var point = new BMap.Point(116.404, 39.915);
map.centerAndZoom(point, 12);
Copy after login

This code A map instance will be created and the map center will be set to the center of Beijing with a zoom level of 12.

Step 4: Add route planning function
Next, we will add route planning function to the map. Baidu Maps provides the BMap.DrivingRoute class to implement route planning functions. The code is as follows:

// 创建DrivingRoute实例
var driving = new BMap.DrivingRoute(map);

// 设置起点和终点
var start = new BMap.Point(116.322, 39.983);
var end = new BMap.Point(116.396, 39.902);

// 设置路线规划参数
var opts = {
    policy: BMAP_DRIVING_POLICY_LEAST_TIME
};

// 规划路线
driving.search(start, end, opts);

// 添加路线到地图
driving.setSearchCompleteCallback(function(results){
    if (driving.getStatus() == BMAP_STATUS_SUCCESS){
        var plan = results.getPlan(0);
        map.addOverlay(new BMap.Polyline(plan.getRoute(0).getPath()));
    }
});
Copy after login

The above code will create a DrivingRoute instance and set the starting point and end point. By setting the BMAP_DRIVING_POLICY_LEAST_TIME parameter, you can choose the route planning strategy. The default is the fastest mode. Then use the search method to plan the route. Finally, add a callback function to add the route to the map.

Step 5: Display route information
If you want to display the text description information of the route on the map, you can use the BMap.RouteLine class. The specific code is as follows:

// 创建RouteLine实例
var routeLine = new BMap.RouteLine(results.getPlan(0).getRoute(0));

// 添加路线到地图
map.addOverlay(routeLine);

// 显示路线信息
routeLine.setTextIcon({
    policy: 'BMAP_DRIVING_POLICY_LEAST_TIME',
    enableDragging: true,
    lineStroke: 6,
    startMarkerStroke: 2,
    endMarkerStroke: 2
});
Copy after login

Through the above code, we can add the route to the map and display the text description information of the route through the setTextIcon method. When displaying text descriptions, you can also customize some style parameters, such as line thickness, starting and ending mark styles, etc.

So far, we have completed all the steps to implement the map route planning function using JS and Baidu Maps. You can expand and adjust the code according to your own needs to achieve more personalized map navigation functions. I hope this article is helpful to you, and I wish you a happy map route planning!

The above is the detailed content of How to use JS and Baidu Maps to implement map route planning 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