Document Maps JavaScript API Introductory Tutorial Add map controls

Add map controls

This article introduces the use of map controls, and the use of plugins can also refer to this article.

Zoom control AMap.ToolBar

Zoom in and out of the map

Scale control AMap.Scale

Display the scale of the current map

Control Compass Widget AMap.ControlBar

Control map rotation and tilt

Positioning ControlAMap.Geolocation

Provides positioning function

Hawkeye Control AMap.HawkEye

Map thumbnail, marking the current display area

Prompt

The JS API provides numerous controls, with map controls being plugins that enhance the interactive experience and functionality of the map. These can display various operational buttons on the map such as scale bars and layer switches to facilitate user interaction and information display.

Map Control Example

1、Implementation steps

Common map controls include: the zoom toolbar ToolBar, the scale Scale, and the geolocation button Geolocation, etc. This example will explain in detail using the zoom toolbar as an example.

1

1.1 Create map

const map = new AMap.Map('container', {
  zoom:11, 
  showOversea: true, //Enable world Map
  center: [77.193045, 28.59086]
});
2

1.2 Introduce map controls

Recommend asynchronous loading method, introduce it where needed. Use the AMap.plugin method to load controls on demand, and use the control functions after the plugin callback.

//Asynchronous Loading Control
AMap.plugin('AMap.ToolBar',function(){ 
  var toolbar = new AMap.ToolBar();
  map.addControl(toolbar);
});
Prompt

After completing this step, the zoom toolbar control has been successfully integrated into the map. Creating other controls follows the same principle. For more controls such as scale bar, overview map, and location, visit the plugin list. For more information on control introduction methods, visit the plugin usage.

3

1.3 Control map widget

For different controls or plugins, there are corresponding instance methods. After the control is instantiated, you can call the corresponding methods to control the control.

toolbar.show(); 
toolbar.hide();