Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program map map


May 18, 2021 WeChat Mini Program Development Document


Table of contents


Base library 1.0.0 starts to support, and low versions need to be compatible.

Maps (v2.7.0 supports same-layer rendering). Related api wx.createMapContext.

Personalized map capabilities can be launched in the background of small programs "development-developer tools-Tencent location services". T he in-program map component should use the same subkey, and different basemap styles can be selected through the layer-style (style number set on the map's official website) property. The length units of component properties default to px, with 2.4.0 supporting incoming units (rpx/px).

Example small program

WeChat small program map map

Property Type The default Required Description The lowest version
longitude number Is Center longs 1.0.0
latitude number Is The center latitude 1.0.0
scale number 16 Whether Zoom level, with a range of 3-20 values 1.0.0
markers Array.<marker> Whether Mark the point 1.0.0
covers Array.<cover> Whether To be removed, use markers 1.0.0
polyline Array.<polyline> Whether Route 1.0.0
circles Array.<circle> Whether Round 1.0.0
controls Array.<control> Whether Control (to be discarded, cover-view instead recommended) 1.0.0
include-points Array.<point> Whether Scale the field of view to include all given coordinate points 1.0.0
show-location boolean false Whether Displays the current anchor point with direction 1.0.0
polygons Array.<polygon> Whether Polygon 2.3.0
subkey string Whether Personalize the key used by the map 2.3.0
layer-style number 1 Whether Style for personalized map configuration, no dynamic modification is supported
rotate number 0 no Rotating angle, range 0 ~ 360, angle of the map of Zhengbei and equipment Y axis 2.5.0
skew number 0 no Tilt angle, range 0 ~ 40, tilt angle about Z axes 2.5.0
enable-3D boolean false no Display 3D floor (tools are not supported) 2.3.0
show-compass boolean false no Show compass 2.3.0
show-scale boolean false no Show the scale, the tool is not supported 2.8.0
enable-overlooking boolean false no Open overlook 2.3.0
enable-zoom boolean true no Whether to support zoom 2.3.0
enable-scroll boolean true Whether Whether drag is supported 2.3.0
enable-rotate boolean false Whether Whether rotation is supported 2.3.0
enable-satellite boolean false Whether Whether to turn on the satellite map 2.7.0
enable-traffic boolean false Whether Whether to turn on real-time traffic 2.7.0
setting object Whether The configuration item 2.8.2
bindtap eventhandle Whether Triggered when clicking on the map, 2.9.0 returns longitude and latitude information 1.0.0
bindmarkertap eventhandle Whether Triggered when clicking on the marker point, e.detail = {markerId} 1.0.0
bindlabeltap eventhandle Whether Triggered when you click on the e.detail = {markerId} 2.9.0
bindcontroltap eventhandle Whether Triggered when you click on the control, e.detail = {controlId} 1.0.0
bindcallouttap eventhandle Whether Trigger e.detail = {markerId} when you click on the bubble corresponding to the marker point 1.2.0
bindupdated eventhandle Whether Triggered when the map render update is complete 1.6.0
bindregionchange eventhandle Whether Triggered when the field of view changes, 2.3.0
bindpoitap eventhandle Whether Triggered when clicking on the map poi point, e.detail = {name, longitude, latitude} 2.3.0

Regionchange returns a value

When the field of view changes, the regionchange is triggered twice and the type value returned is begin/end.

2.8.0 start stage returns causedBy with a valid value of gesture (gesture trigger) and update (interface trigger)

2.3.0 end stage returns causedBy, valid values are drag (drag caused), scale (scale caused), update (call update interface caused) rotate, skew only returns in the end stage

e = {
  causedBy,
  type,
  detail: {
    rotate,
    skew
  }
}

setting

Provides a unified map configuration for setting objects. At the same time, for some animation properties such as rotate and skew, setting separately with setData does not take effect at the same time and needs to be modified uniformly by settting.

// 默认值
const setting = {
  skew: 0,
  rotate: 0,
  showLocation: false,
  showScale: false,
  subKey: '',
  layerStyle: -1,
  enableZoom: true,
  enableScroll: true,
  enableRotate: false,
  showCompass: false,
  enable3D: false,
  enableOverlooking: false,
  enableSatellite: false,
  enableTraffic: false,
}

this.setData({
  // 仅设置的属性会生效,其它的不受影响
  setting: {
    enable3D: true,
    enableTraffic: true
  }
})

marker

Marker points are used to display the location of markers on the map

Property Description Type Required Note The lowest version
Id Mark point id number Whether Marker clicks on the event callback to return this id. er.
latitude Latitude number Is Floating points, range -90 to 90
longitude Longitude number Is Floating points, range -180 to 180
title Label nods string Whether When clicked, the callout is ignored when it exists
zIndex The display hierarchy number Whether 2.3.0
iconPath The icon that appears string Is Picture path in project directory, supporting network path, local path, code pack path (2.3.0)
rotate The angle of rotation number Whether Clockwise angle, range 0 to 360, default to 0
alpha The transparency of the label number Whether Default 1, no transparency, range 0 to 1
width Label icon width number/string Whether The default is the actual width of the picture
height Label the height of the icon number/string Whether The default is the actual height of the picture
callout Customize the bubble window above the marker point Object Whether Supported properties can be found in the table below to identify line breaks. 1.2.0
label Add a label next to the marker point Object Whether Supported properties can be found in the table below to identify line breaks. 1.2.0
anchor Longitude and latitude are at the anchor point of the label icon, the default bottom mid-point Object Whether x, y, x for landscape (0-1), and y for vertical (0-1). The mid-point of the bottom edge is represented by .x: .5, y: 1 1.2.0
aria-label Accessibility, an additional description of the (attribute) element string Whether 2.5.0

Bubble callout on marker

Property Description Type The lowest version
content Text string 1.2.0
color The color of the text string 1.2.0
fontSize The size of the text number 1.2.0
borderRadius Border fillets number 1.2.0
borderWidth The width of the border number 2.3.0
borderColor Border color string 2.3.0
bgColor Background color string 1.2.0
padding The edges of the text are left white number 1.2.0
display 'BYCLICK': Click to show; 'ALWAYS': Always on display string 1.2.0
textAlign Text alignment. Valid values: left, right, center string 1.6.0
anchorX Horizontal offset, positive to the right number 2.11.0
anchorY Longitudinal offset, positive down number 2.11.0

Bubble label on marker

Property Description Type The lowest version
content Text string 1.2.0
color The color of the text string 1.2.0
fontSize The size of the text number 1.2.0
Coordinates of label (abandoned) number 1.2.0
Y Coordinates of label (abandoned) number 1.2.0
anchorX The coordinates of the label, the origin of which is the longitude and latitude corresponding to the marker number 2.1.0
anchorY The coordinates of the label, the origin of which is the longitude and latitude corresponding to the marker number 2.1.0
borderWidth The width of the border number 1.6.0
borderColor Border color string 1.6.0
borderRadius Border fillets number 1.6.0
bgColor Background color string 1.6.0
padding The edges of the text are left white number 1.6.0
textAlign Text alignment. Valid values: left, right, center string 1.6.0

polyline

Specifies a series of coordinate points, from the first item of the array to the last item

Property Description Type Required Note The lowest version
points An array of longitude and latitude array Is [{latitude: 0, longitude: 0}]
color The color of the line string Whether Hexadecimal
width The width of the line number Whether
dottedLine Whether it's a dashed line boolean Whether The default false
arrowLine A line with an arrow boolean Whether The default false, developer tools do not support this property for the time being 1.2.0
arrowIconPath Replace the arrow icon string Whether Effective when arrowLine is true 1.6.0
borderColor The border color of the line string Whether 1.2.0
borderWidth The thickness of the line number Whether 1.2.0

polygon

Specify a series of coordinate points to generate closed polygons based on points coordinate data

Property Description Type Required Note The lowest version
points An array of longitude and latitude array Is [{latitude: 0, longitude: 0}] 2.3.0
strokeWidth The width of the stroke number Whether 2.3.0
strokeColor The color of the stroke string Whether Hexadecimal 2.3.0
fillColor Fill color string Whether Hexadecimal
zIndex Set the polygon Z-axis value number Whether 2.3.0

circle

Show the circle on the map

Property Description Type Required Note
latitude Latitude number Is Floating points, range -90 to 90
longitude Longitude number Is Floating points, range -180 to 180
color The color of the stroke string Whether Hexadecimal
fillColor Fill color string Whether Hexadecimal
radius Radius number Is
strokeWidth The width of the stroke number Whether

control

Displays controls on the map that do not move with the map. To be abandoned, use cover-view

Property Description Type Required Note
Id Control id number Whether Clicking on the control event callback returns this id
position Control where the map is located object Is Control relative to the map location
iconPath The icon that appears string Is Picture paths in the project catalog, supporting local paths, code pack paths
clickable Whether it's clickable boolean Whether It is not clickable by default

position

Property Description Type Required Note
left How far away is the left boundary of the map number Whether The default is 0
top How far from the top border of the map number Whether The default is 0
width The width of the control number Whether The default is the width of the picture
height Control height number Whether The default is the picture height

Bindregionchange returns a value

Property Description Type Note
type The field of view changes start and end string The field of view change begins with begin and ends with end
causedBy The cause of the change in field of vision string Dragging the map causes (drag), zoom causes (scale), calls the interface causes (update)

Bug & Tip

  1. tip: Personalized maps do not support debugging in tools at this time. Please test with the WeChat client first.
  2. tip: Color values in the map, such as color/borderColor/bgColor, need to be represented by 6 bits (8 bits) heteen, and the last two of the 8 bits represent alpha values, such as #000000AA
  3. tip: The latitude and longitude of the map assembly is required, and if you do not fill in the longitude and latitude, the default value is the latitude and longitude of Beijing.
  4. tip: The map component uses a Martian coordinate system, and calling the wx.getLocation interface requires specifying type as gcj02
  5. tip: Map supports same-layer rendering from 2.8.0, please refer to native component usage restrictions for more information
  6. tip: Be aware of the restrictions on the use of native components.

Scale

scale 3 4 5 6 7 8 9 10 11
Proportion 1000km 500km 200km 100km 50km 50km 20km 10km 5km
scale 12 13 14 15 16 17 18 19 20
Proportion 2km 1km 500m 200m 100m 50m 50m 20m 10m

The sample code

Preview the effect in the developer tool

<!-- map.wxml -->
<map id="map" longitude="113.324520" latitude="23.099994" scale="14" controls="{{controls}}" bindcontroltap="controltap" markers="{{markers}}" bindmarkertap="markertap" polyline="{{polyline}}" bindregionchange="regionchange" show-location style="width: 100%; height: 300px;"></map>
// map.js
Page({
  data: {
    markers: [{
      iconPath: "/resources/others.png",
      id: 0,
      latitude: 23.099994,
      longitude: 113.324520,
      width: 50,
      height: 50
    }],
    polyline: [{
      points: [{
        longitude: 113.3245211,
        latitude: 23.10229
      }, {
        longitude: 113.324520,
        latitude: 23.21229
      }],
      color:"#FF0000DD",
      width: 2,
      dottedLine: true
    }],
    controls: [{
      id: 1,
      iconPath: '/resources/location.png',
      position: {
        left: 0,
        top: 300 - 50,
        width: 50,
        height: 50
      },
      clickable: true
    }]
  },
  regionchange(e) {
    console.log(e.type)
  },
  markertap(e) {
    console.log(e.detail.markerId)
  },
  controltap(e) {
    console.log(e.detail.controlId)
  }
})