map

This topic introduces the map components. If multiple map components are displayed on the same page, different IDs are required. The map components are the native components in an app, which have the highest level of the hierarchy.

Note: For related API, see my.createMapContext(mapId).

Prerequisites

Before you begin, make sure you have understood the following prerequisites:

  • Do not use the map components in scroll-view.
  • The map components do not support CSS animation.
  • If the user zooms in or zooms out the map, reset the value of scale in the onRegionChange function before setting the latitude and longitude of the location. Otherwise, the map restores to the original size. See sample codes of regionchange for details.
  • The mini program does not support obtaining the latitude and longitude of the current map.

Sample codes

See the following sample codes of the map component:

copy
<view>
 <map id="map" longitude="103.855457" latitude="1.339712" scale="{{scale}}"
 markers="{{markers}}"
 onMarkerTap="markertap"
 polyline="{{polyline}}"
 circles="{{circles}}"
 onRegionChange="regionchange"
 onTap="tap"
 show-location style="width: 100%; height: 300px;"
 include-points="{{includePoints}}"></map>
 <button onTap="changeScale">changeScale</button>
 <button onTap="getCenterLocation">getCenterLocation</button>
 <button onTap="moveToLocation">moveToLocation</button>
 <button onTap="changeCenter">changeCenter</button>
 <button onTap="changeMarkers">changeMarkers</button>
</view>
copy
Page({
  data: {
    scale: 14,
    longitude: 103.855457,
    latitude: 1.339712,
    markers: [{
      iconPath: "/image/green_tri.png",
      id: 10,
      latitude:  1.342983,
      longitude: 103.867935,
      width: 50,
      height: 50
   },
              {
      iconPath: "/image/green_tri.png",
      id: 11,
      latitude: 1.343573,
      longitude: 103.861916,
      width: 50,
      height: 50,
   }],
    includePoints: [{
      latitude: 1.347016,
      longitude: 103.860167,
   }],
    polyline: [{
      points: [{
        longitude: 103.863218,
        latitude: 1.351628
     }, {
        longitude: 103.862718,
        latitude: 1.351428
     }, {
        longitude: 103.862218,
        latitude: 1.350828
     }, {
        longitude: 103.861718,
        latitude: 1.350428
     }, {
        longitude: 103.861018,
        latitude: 1.351028
     }],
      color: "#FF0000DD",
      width: 5,
   }],
    circles: [{
      latitude: 1.351628,
      longitude: 103.863718,
      color: "#000000AA",
      fillColor: "#000000AA",
      radius: 80,
      strokeWidth: 5,
   }],
 },
 
 onReady(e) {
   // Use my.createMapContext to obtain the map context.
   this.mapCtx = my.createMapContext('map')
 },
 
 getCenterLocation() {
   this.mapCtx.getCenterLocation(function (res) {
      console.log(res.longitude)
      console.log(res.latitude)
   })
 },
 
 moveToLocation() {
   this.mapCtx.moveToLocation()
 },
 
 regionchange(e) {
    console.log('regionchange', e);
// Note: If the user zooms in or zooms out the map, reset the value of scale of the onRegionChange function before setting the latitude and longitude of the location. Otherwise the map restores to the original size.
if (e.type === 'end') {
     this.setData({
        scale: e.scale
     });
   }
 },
 
 markertap(e) {
    console.log('marker tap', e);
 },
 
 controltap(e) {
    console.log('control tap', e);
 },
 
 tap() {
    console.log('tap:');
 },
 
 changeScale() {
   this.setData({
      scale: 8,
   });
 },
 
 changeCenter() {
   this.setData({
      longitude: 103.867935,
      latitude: 1.343573,
      includePoints: [{
        latitude: 1.351028,
        longitude: 103.861018,
     }],
   });
 },
 //An indicator of whether to support gesture events. When isGestureEnable is 1, gesture events are supported. Otherwise gesture events are not supported.
 gestureEnable() {
   this.mapCtx.gestureEnable({isGestureEnable:1});
 },
 //An indicator of whether to show the compass. When isShowCompass is 1, display the compass. Otherwise the compass is not displayed.
 showsCompass() {
   this.mapCtx.showsCompass({isShowsCompass:1});
 },
 changeMarkers() {
   this.setData({
      markers: [{
        iconPath: "/image/green_tri.png",
        id: 10,
        latitude: 1.351028,
        longitude: 103.861018,
        width: 50,
        height: 50
     }],
      includePoints: [{
        latitude: 1.350428,
        longitude: 103.861718,
     }],
   });
 },
})

Parameters

Property

Type

Description

id

String

The ID to identify the map component.

style

String

Inline style.

latitude

Number

The latitude of the central point.

longitude

Number

The longitude of the central point.

scale

Number

The zoom level. The value ranges from 5 to 18 and is 16 by default.

markers

Array

The location marker. See markers for details.

polyline

Array

The polyline. See polyline for details.

circles

Array

The circle. See circles for details.

polygon

Array

The polygon. See polygon for details.

show-location

Boolean

An indicator of whether to display the current location. When the value is true, the current location is displayed.

include-points

Array

The view is extended on a small scale with the passed coordinates.

Example:

[{

latitude: 1.350428,

longitude: 103.861718,

}]

include-padding

Object

The view is displayed within the map padding.

Example:

[{

left:0, right:0,

top:0, bottom:0

}]

setting

Object

Settings.

Example:

{

gestureEnable: 1,


showScale: 1,


showCompass: 1,


tiltGesturesEnabled: 1,

trafficEnabled: 0,


showMapText: 0

}

onMarkerTap

EventHandle

Call this function when clicking on Marker.

Example:

{

markerId,

latitude,

longitude,

}

onRegionChange

EventHandle

Call this function when the view is changed.

Example:

{

type: "begin/end",

latitude,

longitude,

scale

}

onTap 

EventHandle

Call this function when clicking on the map.

Example:

{

latitude,

longitude,

}

markers

Specify a location marker that identifies a location on the map.

Notes:

  • With the markers parameter, multiple location markers can be displayed.
  • The description of the location marker does not support English.

Property

Description

Type

Required

id

The ID of the location marker, when is returned in the click event callback.

Number

Yes

latitude

The latitude of the location.

Value range: -90 - 90

Float

Yes

longitude

The longitude of the location.

Value range: -180 - 180

Float

Yes

width

The actual width of the image by default.

Number

No

height

The actual height of the image by default.

Number

No

alpha

The transparency of the image. By default, the value is 1, which means the image is totally opaque. A value of 0 means totally transparent.

Number

No

iconPath

The image path in the project directory, which cannot be a relative path, but can only be an absolute path beginning with /.

For example: /pages/image/test.jpg

String

Yes

label

See label for details.

Object

No

polygon

Specify a series of coordinates, which form a closed polygon based on the points.

Property

Description

Type

Required

points

An array of latitude and longitude.

Example:

[{

latitude: 0,

longitude: 0

}]

Array

Yes

color

The stroke color. Use hexadecimal numbers to set colors.

Example: #eeeeeeAA

String

No

fillColor

The fill color. Use hexadecimal numbers to set colors.

Example: #eeeeeeAA

String

No

width

The stroke width.

Number

No

polyline

Specify a series of coordinates, which are connected from the first item to the last item in an array.

Property

Description

Type

Required

points

An array of latitude and longitude. 

Example:

[{

latitude: 0,

longitude: 0

}]

Array

Yes

color

The stroke color. Use hexadecimal numbers to set colors.

Example: #eeeeeeAA

String

No

width

The stroke width.

Number

No

circles

Display a circle on the map.

Property

Description

Type

Required

latitude

The latitude. The value ranges from -90 to 90.

Float

Yes

longitude

The longitude. The value ranges from -180 to 180.

Float

Yes

color

The stroke color. Use hexadecimal numbers to set colors.

Example: #eeeeeeAA

String

No

fillColor

The fill color. Use hexadecimal numbers to set colors. 

Example: #eeeeeeAA

String

No

radius

The radius in meters. 

Number

Yes

strokeWidth

The stroke width.

Number

No

Location marker design

label

Property

Required

Note

content

Yes

-

color

No

The default value is #000000.

fontsize

No

The default value is 14.

borderRadius

No

The default value is 20.

bgColor

No

The default value is #FFFFFF.

padding

No

The default value is 10.

FAQs

How do the map components redirect the mini program to the Google Maps for the navigation?

Use the my.openLocation API.

How to obtain the value of scale when the optimize property of the map components is true?

Use the onRegionChange function.

How to create the polygon area on the map manually?

Use the polygon property.

How to modify icons of the first item and the last item in the map components after iconPath is set?

Currently the modification is not supported.

More information

my.createMapContext

MapContext Overview