Autonavi JS API summary

Introduction to the

This is the recent use of Amap JS API to do a summary, about each class can achieve what effect, and how to use. After that, the optimization ideas and solutions of some common problems will be summarized.

This blog address – https://encaik.gitee.io/blog/amap.html

Amap API is introduced in Vue

  • NPM package introduced
npm i amap-js -S
Copy the code

Advantages: Out of the box, easy to use

  • Script Introduction

Add a link to index.html, followed by the requested key.

<script
  type="text/javascript"
  src="Https://webapi.amap.com/maps?v=1.4.15&key= [key]." "
></script>
<script src="https://webapi.amap.com/ui/1.1/main.js"></script>
Copy the code

Configure the global name in vue.config.js, which can be called directly from the vue instance in code.

module.exports = {
  configureWebpack: {
    externals: {
      AMap: "AMap".AMapUI: "AMapUI"}}; }Copy the code

Advantages: High degree of customization, you can combine functions as required.

Basic concepts of Amap

The map

Map is the bearer of Amap API, all APIS need to be received by a map, the same interface can have multiple maps.

<! - map -- -- >
<div id="container" style="width: 100%; height: 100%"></div>
Copy the code
this.map = new this.AMap.Map("container", {
  resizeEnable: true.// Whether to monitor map container size changes
  zoom: 15.// Initialize the map hierarchy
  zooms: [5.18].// Map allows scaling levels
  center: [117.96.40.96].// Initializes the map center point
  mapStyle: "amap://styles/407154d906b9cd83d4e5e57df8b27cec" // Customize the map publishing style
});
Copy the code

The layer

Layers are layers covered on the map, and each layer has a different function. There is a hierarchical relationship between layers.

  • The official layer

The name of the class instructions Whether or not the plugin
AMap.TileLayer Slice layer class no
AMap.TileLayer.Satellite Satellite layer class, inherited from TileLayer no
AMap.TileLayer.RoadNet The Road network layer class inherits from TileLayer no
AMap.TileLayer.Traffic Real time traffic layer class, inherited from TileLayer no
AMap.Buildings Block layer, a layer that independently displays vector block data no
AMap.MassMarks Image volume dot layer class no
AMap.Heatmap Thermal map plug-in is
AMap.LayerGroup Layer collection, used to wrap instances of other layer classes and perform batch operations on the collection no
AMap.LabelsLayer Annotation layer, used to add LabelMarker type annotation no
/ / satellite
let satellite = new AMap.TileLayer.Satellite();
/ / network
let road = new AMap.TileLayer.RoadNet();
this.map.setLayers([satellite, road]);
Copy the code
  • Self-built layer

The name of the class instructions Whether or not the plugin
AMap.TileLayer.Flexible Custom slice layer, you can flexibly customize slice content raster layer, inherited from amap.tilelayer no
AMap.ImageLayer Image layer, can be overlaid on the map of the corresponding area no
AMap.CanvasLayer Canvas layer, which can be superimposed on the corresponding area of the map no
AMap.VideoLayer The “Video” layer is used to superimpose the Video on the corresponding area of the map no
AMap.CustomLayer Completely custom drawn layers no
let imageLayer = new AMap.ImageLayer({
  url:
    "http://amappc.cn-hangzhou.oss-pub.aliyun-inc.com/lbs/static/img/dongwuyuan.jpg".bounds: new AMap.Bounds([116.327911.39.939229], [116.342659.39.946275])});Copy the code
  • WMS/WMTS

The name of the class describe Whether or not the plugin
AMap.TileLayer.WMS Used to load OGC standard WMS layers no
AMap.TileLayer.WMTS Used to load OGC standard WMTS layers no
var wms = new AMap.TileLayer.WMTS({
  url:
    "https://services.arcgisonline.com/arcgis/rest/services/Demographics/USA_Population_Density/MapServer/WMTS/".blend: false.tileSize: 256.params: {
    Layer: "0".Version: "1.0.0".Format: "image/png".TileMatrixSet: "EPSG:3857"}});Copy the code

mulch

Coverings are vector shapes or ICONS drawn on a map.

  • Marker class

Dot marks, ICONS can be customized.

  • The Text class

Text annotation, you can add custom style text.

  • Polyline class

Broken line

  • Polygon class

polygon

  • BezierCurve class

Bessel curve

  • Circle the class

circular

  • OverlayGroup class

OverlayGroup is the overlay management class. You can use this class to batch manage the display and hide of the overlay and other operations.

Information form

The info form is an HTML node that appears on the interface based on the map location offset.

this.infoWindow = new this.AMap.InfoWindow({
  isCustom: true.autoMove: true.closeWhenClickMap: true.anchor: "top-left".content: document.getElementById("infoWindow"),
  offset: new this.AMap.Pixel(22.22)});Copy the code

The map controls

Map controls are officially packaged components used to control map operations.

Controls the name instructions Whether or not the plugin
AMap.ControlBar A combination of map controls such as rotation, tilt, reset and zooming will now display in 3D map mode (added since V1.4.0) is
AMap.MapType Map type toggle plugin, used to toggle fixed several commonly used layers is
AMap.OverView Map Hawk-eye plugin, default in the lower right corner of the map display thumbnail is
AMap.Scale Map scale plug-in is
AMap.ToolBar Map toolbar plug-in, can be used to control the map zoom and pan is
this.map.plugin(["AMap.ControlBar"].() = > {
  let controlBar = new AMap.ControlBar(Options);
  this.map.addControl(controlBar);
});
Copy the code

tool

The plug-in name instructions Whether or not the plugin
AMap.MouseTool Mouse tool plug-in is
AMap.CircleEditor Circular editor plugin for editing amap. Circle objects is
AMap.PolyEditor Polyline and polygon editing plug-in is
AMap.BezierCurveEditor Bezel curve editor plug-in is
AMap.EllipseEditor Ellipse editing plug-in is
AMap.RectangleEditor Rectangle editor plug-in is
AMap.Hotspot Hot map is
AMap.MarkerClusterer Point aggregation plug-in is
AMap.RangingTool Range measurement plug-in is

Event listeners

Use the on(eventName, Handler, Context) and off(eventName, Handler, Context) methods to listen for maps and map elements.

EventName: eventName (mandatory)

Handler: Event callback function (required)

Context: The context in the event callback (optional, by default, this in handler is the object calling on, otherwise this refers to the object referenced by context)

Note: In multiple bindings, the eventName, handler function object, and Context object will be bound again if any of them are different.