Route planning
This article introduces the use of the Driving Planning API, and the AMap.Driving plugin is required for Compute Routes For Driving.
Compute Routes For Driving Example
1、Implement Compute Routes For Driving
1.1 Create a map
const map = new AMap.Map("container", {
viewMode: "2D",
zoom: 11,
showOversea: true, //Enable world Map
center: [100.491422, 13.749313],
});1.2 Confirm the start and end point information of the plan
There are generally two ways to obtain Compute Routes For Driving: keywords and latitude and longitude. These two methods will be introduced separately below.
- If you use place names to plan the start and end points, the format is as follows:
const points = [
{ keyword: '2 Na Phra Lan Rd, Khwaeng Phra Borom Maha Ratchawang', city: '764010000' }, //Starting point coordinates
{ keyword: '34 Wang Doem Rd, Khwaeng Wat Arun', city: '764010000' } //Destination coordinates
]- If you use the latitude and longitude of the location to confirm the start and end points, the format is as follows:
const startLngLat = [100.491422, 13.749313]; //Starting point coordinates
const endLngLat = [100.49, 13.74]; //Destination coordinates1.3 Introduce and create a Compute Routes For Driving plugin, obtain the start and end point route
It is recommended to use asynchronous installation plugins, the method of plugin introduction and plugin usage.
- Use location names to plan routes, format as follows
//Introduce and create a Compute Routes For Driving plugin
AMap.plugin(["AMap.Driving"], function () {
const driving = new AMap.Driving({
map: map,
panel: "my-panel", //The parameter value is the ID of the container defined in your page
});
//Get the route planning from starting point to destination
driving.search(points, function (status, result) {
if (status === "complete") {
//status: complete indicates a successful query, no_data indicates no results found, and error represents a query error
//When the query is successful, the result is the corresponding driving navigation information
console.log(result);
} else {
console.log("Failed to obtain driving data:" + result);
}
});
});- Use latitude and longitude planning to obtain the planned route, the format is as follows
AMap.plugin(["AMap.Driving"], function () {
const driving = new AMap.Driving({
map: map,
panel: "my-panel", //The parameter value is the ID of the container defined in your page
});
//Get the route planning from starting point to destination
driving.search(startLngLat, endLngLat, function (status, result) {
if (status === "complete") {
//status: complete indicates a successful query, no_data indicates no results found, and error represents a query error
//When the query is successful, the result is the corresponding driving navigation information
console.log(result);
} else {
console.log("Failed to obtain driving data:" + result);
}
});
});The result of the route planning will be displayed on the specific panel specified by the parameter panel: 'my-panel'.
The types of route planning in JS API are: driving AMap.Driving, transit AMap.Transfer, and walking AMap.Walking.
So far, you have successfully implemented a simple Compute Routes For Driving feature using JS API . Learn more by visiting the advanced route planning tutorial.