Document Maps JavaScript API Reference Manual Routes Driving

Driving

AMap.Driving

Driving route planning service, providing the functionality to query driving navigation routes based on start and end coordinates.

Construct a driving route planning service object, and set its properties through DrivingOptions. DrivingOptions allows setting driving strategies and the level of detail in returned information. Users can retrieve and display query results through custom callback functions. If the service request fails, the system will return error information.

new AMap.Driving(opts: DrivingOptions)

Parameter

opts(DrivingOptions)Driving route planning parameters

Attribute

Type

Description

map

Map

AMap.Map object, the map instance to display results. When this parameter is specified, the markers and routes of the search results will be automatically added to this map. Optional

policy

number?

Driving route planning strategy

extensions

string

Default value: base, returns basic address information. If the value is all, returns DriveStep basic information + DriveStep detailed information

panel

string | HTMLElement

The HTML container id or container element for the result list. After providing this parameter, the result list will be displayed in this container. Optional

hideMarkers

boolean

Set whether to hide the start and end point icons of the route planning. Set to true: hide icons; set to false: show icons. Default value: false

showTraffic

boolean?

Whether to display real-time traffic information, default is true. Green indicates smooth traffic, yellow indicates slight congestion, red indicates heavy congestion, and gray indicates no traffic information

isOutline

boolean?

Whether to display the stroke of the planned route when using the map property. Default is true

outlineColor

string?

The stroke color of the planned route when using the map property. Default is 'white'

autoFitView

boolean?

Used to control whether to automatically adjust the map view after route planning to ensure the drawn route is within the visible range of the viewport

Demo

AMap.plugin("AMap.Driving", function () {
  var drivingOptions = {
    map: map,
    autoFitView: true,
  };
  var driving = new AMap.Driving(drivingOptions);
  driving.search([
    { keyword: '2 Na Phra Lan Rd, Khwaeng Phra Borom Maha Ratchawang', city: '764010000' },
    { keyword: '34 Wang Doem Rd, Khwaeng Wat Arun', city: '764010000' },
  ]);
});

Method

search(origin, destination, opts, callback)

Implement driving route planning based on the coordinates or names of the starting point, destination, and optional waypoints, with waypoints set via opts

Parameter:

origin (LngLatStarting Point Latitude and Longitude

destination (LngLatDestination Latitude and Longitude

opts (Object)

Attribute

Type

Description

waypoints

Array<LngLat>

Waypoints, up to 16 supported

callback (DrivingCallbackWhen the status is complete, the result is DrivingResult; when the status is error, the result is the error message info; when the status is no_data, it means the search returned 0 results

Demo:

const startLngLat = [100.491422, 13.749313]; //Starting point coordinates
const endLngLat = [100.49, 13.74]; //Destination coordinates
var opts = {
  waypoints: [[100.482731, 13.745401]], //Set waypoint parameters, supports up to 16 waypoints
};
driving.search(startLngLat, endLngLat, opts, function (status, result) {
  //status: complete indicates a successful query, no_data indicates no results, and error represents a query error
 //When the query is successful, the result is the corresponding driving navigation information
});

search(points, callback)

Based on the coordinates or names of the start point, end point, and waypoints (optional), implement driving route planning, with waypoints set through opts

Parameter:

points (Array<Object>The latitude and longitude points of the destination are an array of the names and corresponding cities of the start point, end point, and waypoints (optional)

callback (DrivingCallbackWhen the status is complete, the result is DrivingResult; when the status is error, the result is the error message info; when the status is no_data, it means the search returned 0 results

Demo:

var points = [
  { keyword: '2 Na Phra Lan Rd, Khwaeng Phra Borom Maha Ratchawang', city: '764010000' }, //Starting point coordinates
  { keyword: '198 Maha Rat Rd, Khwaeng Phra Borom Maha Ratchawang', city: '764010000' }, //Set waypoint parameters, supports up to 16 waypoints
  { keyword: '34 Wang Doem Rd, Khwaeng Wat Arun', city: '764010000' } //Destination coordinates
  ];
];
driving.search(points, function (status, result) {
  //status: complete indicates a successful query, no_data indicates no results, and error represents a query error
 //When the query is successful, the result is the corresponding driving navigation information
});

clear()

Clear search results

Demo:

driving.clear();

setPolicy(policy)

Set driving route planning strategy

Parameterpolicy (numberDriving route planning strategy

Demo:

driving.setPolicy(0);

Event

Event Name

Description

complete

This event is triggered when the query is successful (Type: DrivingResult)

error

This event is triggered when the query fails (Type: ErrorStatus)