This is the 23rd day of my participation in the August More Text Challenge.More challenges in August

For development to do, the map function is often encountered, so after my consideration to use Baidu map

Because Baidu Map JavaScript API GL V1.0 is a set of application program interface written by JavaScript language, can help you build rich and interactive map application in the website, support PC and mobile browser based map application development, and support HTML5 features of map development.

Baidu Map JavaScript API support HTTP and HTTPS, free of charge, can be used directly. The number of times the interface can be used is unlimited. Before using it, you need to apply for a key (AK). Before you use baidu Map JavaScript API, please read baidu Map API Terms of use. For any non-profit application, please use it directly. For commercial application, please refer to the instructions.

JavaScript API GL uses WebGL to render maps, coverings, etc., and supports 3D viewing of maps. GL version interface is basically backward compatible with low migration cost. The current V1.0 version supports basic 3D map displays, basic map controls and coverings.

So before using baidu map, we will go to the website, baidu map lbsyun.baidu.com/apiconsole/… Register and create specific applications to obtain the AK accessing the application. As follows:

Use the sample

Start by installing dependency packages in your project

npm i vue-baidu-map --save
Copy the code

Imported in the entry file main.js

import BaiduMap from 'vue-baidu-map'; Vue. Use (BaiduMap, {ak: 'your BaiduMap key ak'})Copy the code

Then add a reference to the index.html entry file

<script type="text/javascript" SRC ="//api.map.baidu.com/api?v=1.1&ak= your AK"></script>Copy the code

Then create a vUE single page to add

<template> <div> <div id="map" style="width:1200px; height:500px;" ></div> </div> </template>Copy the code

In Mounted, write the method for creating a MAP instance

Var map = new map.map ("map"); CenterAndZoom (new map.point (113.370171,23.129587), 15); Map. addControl(new map. MapTypeControl({mapTypes:[BMAP_NORMAL_MAP, BMAP_HYBRID_MAP ]})); Map. SetCurrentCity (" guangzhou "); / / set the map shows the city This is you must set up the map. EnableScrollWheelZoom (true); // Enable mouse wheel zoomingCopy the code

If you want to remove the baidu Map logo, you can add the following in the CSS

.BMap_cpyCtrl {
    display: none;
}
.anchorBL {
    display: none;
}
Copy the code

Now you can create a map module!

parameter

methods The return value describe
enableDragging() none Enable map drag, enabled by default
disableDragging() none Disable map drag and drop
enableInertialDragging() none Enable map inertial drag, disabled by default
disableInertialDragging() none Disable map inertial drag
enableScrollWheelZoom() none Allows maps to be zoomed by mouse wheel, disabled by default
disableScrollWheelZoom() none Disables map zooming by mouse wheel
enableContinuousZoom() none Enable double click smooth zoom effect
disableContinuousZoom() none Turn off the double click smooth zoom effect
enableResizeOnCenter() none The center point of open map resize remains unchanged
disableResizeOnCenter() none Resize center point of closed map remains unchanged
enableDoubleClickZoom() none Enable map double – click zoom, left – click zoom, right – click zoom
disableDoubleClickZoom() none Cancel the map double-click zoom
enableKeyboard() none Enable keyboard operations. By default, keyboard operations are disabled. The top, bottom, left and right keys of the keyboard can move the map continuously. Pressing two of these keys simultaneously moves the map diagonally. The PgUp, PgDn, Home, and End keys shift the map by 1/2 of its size. The + and – keys make the map zoom in or out one level
disableKeyboard() none Disabling keyboard operations
enablePinchToZoom() none Enable two-finger zoom map.
disablePinchToZoom() none Disables two-finger zoom maps.
enableRotateGestures() none Whether it is possible to rotate the map using gestures.
enableTiltGestures() none Whether it is allowed to tilt the map with gestures.
enableAutoResize() none Enable Automatic container size adaptation. This function is enabled by default
disableAutoResize() none Disable auto adaptation to container size changes
checkResize() none This method is called to relay the map after the map container changes
resize() none Forces the map to be resized, at which point the image data required for the view is recalculated and redrawn against the current container size. When auto View is turned off (enableAutoResizeConfig), you need to call this method to force a map refresh.
getSize() Size Returns the current size of the map, in pixels
getContainerSize() Size Gets the map container size
getZoomUnits() Number Returns the current map level and how many units of flat Mercator coordinates a pixel corresponds to
getContainer() HTMLElement Returns the DOM container element of the map. Map containers are obtained by implementing the Control.initialize() method and adding the container elements of the Control to the map when creating user-defined controls
pixelToPoint(pixel: Pixel ) Point Pixel coordinates are converted to latitude and longitude coordinates
pointToPixel(point: Point ) Pixel Convert latitude and longitude coordinates to pixel coordinates
lnglatToMercator(lng: Number, lat: Number) [McLng, McLat] Latitude and longitude sphere coordinates are converted to Mercator plane coordinates
mercatorToLnglat(McLng: Number, lat: McLat) [lng, lat] Mercator plane coordinates are converted to latitude and longitude spherical coordinates
isLoaded() boolean Returns whether the map was initialized by centerAndZoom
addSpots(spots: Array, options: Object) number Add location areas as virtual clickable areas on the map. Parameter spots indicates the hot zone point group, and options is optional. Returns the area ID.
getSpots(id: string) Array Returns an array of locations based on id
removeSpots(id: number) none Removes array of regions by ID
clearSpots() none Clears the site area, which clears all virtual pointable data
clearLabels() none Clear all custom base map labels in the current map
addLabelsToMapTile(labels:Array) none Add text to the base map, these text and the base map text will participate in dodge.
removeLabelsFromMapTile(labelUids:Array) none Removes the text annotation from the base image, taking uid array as the parameter and removing it according to the UID in the array
getIconByClickPosition(clickPosition: Pixel) Object null
setBounds(bounds:Bounds) none Set dragable area of the map. Parameter is the range of dragable area of the map
getBounds() Bounds Gets the rectangular region of the map’s current field of view, expressed in geographic coordinates. Returns an empty map if the map has not been initializedBoundsInstance.
getCoordType() string Gets the map coordinate type, CoordType constant
getMapStyleId() string Gets the current map style ID, or the style name for the built-in style; For custom styles, the internally generated style ID is returned
getPanes() MapPanes Gets the overlay container element that returns the map overlay container object
getInfoWindow() InfoWindow null
setDefaultCursor(cursor: String) none Sets the default mouse pointer style for the map. The parameter cursor must comply with the CSS cursor property specification
getDefaultCursor() String Gets the default mouse pointer style of the map, returning a cursor value
setDraggingCursor(cursor: String) none Sets the mouse pointer style for dragging and dropping a map. The parameter cursor must comply with the CSS cursor property specification
getDraggingCursor() String Returns the mouse pointer style for dragging and dropping the map
setMinZoom(zoom: Number) none Sets the minimum level allowed on the map. The value must be at least the minimum level allowed by the map type
setMaxZoom(zoom: Number) none Sets the maximum level allowed on the map. The value cannot be greater than the maximum level allowed by the map type
getDistance(start: Point , end: Point ) Number Returns the distance between two points in meters
getMapType() MapTypeId Return map type
setViewport(view: Array  Viewport , viewportOptions: ViewportOptions ) none
getViewport(view: Array< Point >, viewportOptions: ViewportOptions ) Viewport The best map view is obtained according to the geographic region or coordinates provided. The returned object contains the Center and Zoom attributes, representing the center point and level of the map, respectively. This method only returns the view information and does not apply the new center point and level to the current map
centerAndZoom(center: Point , zoom: Number) none Let’s initialize the map. If the center type is Point, zoom must be assigned levels 3-19. If the hd base map (developed for mobile terminals) is called, Zoom can be assigned levels 3-18. If the center type is a string, such as “Beijing”, zoom can be ignored, and the map will automatically match the best zoom level according to Center
panTo(center: Point ) none Change the center point of the map to the given point, jump to the specified center point for rendering. If the point is already visible in the current map view, it is moved to the center point in a smooth animation. You can configure to force movement without animation
panBy(x: Number, y: Number) none Move the map x pixels horizontally and y pixels vertically. If the specified pixel is larger than the viewable area or if no animation is specified in the configuration, the slide effect is not performed
flyTo(center: Point , zoom: Number) none Fly to the specified center point and level, available for use on the positioning zoom map
reset() none Reset the map to restore the center point and level of the map when it was initialized
setCenter(center: Point  String[, options: Object]) none
getCenter() Point Returns the current center point of the map
setMapType(mapTypeId: MapTypeId ) none Setting map Type
setZoom(zoom: Number[,options]) none Switches the view to the specified zoom level, leaving the center point coordinates unchanged. Note: When an information window is open on the map, map zooming will keep the coordinate position of the information window unchanged. Optional configuration parameters include ‘noAnimation: Boolean ‘whether animation is disabled; This method is called after the ‘callback:function’ animation ends, or immediately if there is no animation; ‘zoomCenter: Point’ zooms to the center Point of the map by default
getZoom() Number Returns the current zoom level of the map
zoomIn() none Zoom in level 1 view
zoomOut() none Zoom out level 1 view
addControl(control: Control ) none To add a control to a map, a control instance can only be added to the map once
removeControl(control: Control ) none Removes the control from the map. This removal has no effect if the control was never added to the map
addContextMenu()(menu: ContextMenu ) none Add right-click menu
removeContextMenu()(menu: ContextMenu ) none Remove right-click menu
addOverlay(overlay: Overlay ) none To add a overlay to a map, an instance of a overlay can only be added to the map once
removeOverlay(overlay: Overlay ) none Remove overlay from map. If the overlay was never added to the map, the removal has no effect
clearOverlays() none Clear all overlay from the map
pointToOverlayPixel(point: Point ) Pixel Get the coordinates of the corresponding coverings container based on geographic coordinates. This method is used to customize coverings
overlayPixelToPoint(pixel: Pixel ) Point Obtain the corresponding geographic coordinates according to the coordinates of the covering container
getOverlays() Array< Overlay > Gets all overlay objects on the current map, returning a collection of overlay objects
getPanes() MapPanes Returns the list of map overlay containers
getCurrentMaxTilt() number Gets the maximum tilt Angle allowed on the current map
hightlightSpotByUid(uid: string, tilePosStr: string) none The POI on the base map is highlighted based on uid, where the parameter tilePosStr is the position string of label
resetSpotStatus() none Reset hot spot status, to unhighlight hot spot
addAreaSpot() none Reset hot spot status, to unhighlight hot spot
getAreaSpot(id: string) Array Returns an array of locations
removeAreaSpot(id: string) none Remove array of regions
clearAreaSpots() none Clears the site area, which clears all virtual pointable data
setTrafficOn() none Turn on the traffic layer
setTrafficOff() none Close the road condition layer
showOverlayContainer() none Display overlay
hideOverlayContainer() none Cover is not shown
setMapStyleV2(config: Object) none Set the personalization map to personalization configuration object
startViewAnimation(viewAnimation: ViewAnimation) Number Start perspective animation
cancelViewAnimation(viewAnimation: ViewAnimation) none Stop perspective animation
getMapScreenshot() url Get map screenshots. Earth mode is not supported. PreserveDrawingBuffer: true is required to initialize the map, otherwise black screen is displayed
loadMapStyleFiles(callback: Function) none The style file required to load the current style of the map, callback is the callback function after loading successfully
setCopyrightOffset(logo: Object, cpy: Object) none Set the location of copyright information. Logo is the location of logo, and copyright is the location of text
destroy() none Destroy the map. When rendering the map using WebGL, if you confirm that the map instance is no longer used, you need to call this method to destroy the WebGL context. Otherwise, frequent creation of new map instances will cause the browser to send a warning: too many WebGL Context
isSupportEarth() boolean Check whether the browser supports earth, return true if it does, false otherwise