Represents the map component.
Before using this component, make preparations by referring to Preparations.
To display the user location on the map, you need to apply for the location permission or use the location component. For details about how to apply for the permission, see Enabling the my-location Icon.
Since: 1.0.0
Related document: has.createMapContext
Attributes
| Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| latitude | number | 39.908692 | Yes | Latitude of the map's center point. The default value is the latitude of Tiananmen. Note: Use gcj02 for the Chinese mainland and Hong Kong and Macao regions, and use wgs84 for other regions. |
| longitude | number | 116.397477 | Yes | Longitude of the map's center point. The default value is the longitude of Tiananmen. Note: Use gcj02 for the Chinese mainland and Hong Kong and Macao regions, and use wgs84 for other regions. |
| scale | number | 16 | No | Zoom level. The value ranges from 2 to 20. |
| rotate | number | 0 | No | Rotation angle. The value ranges from 0 to 360, in degrees. Since: 2.0.2 |
| skew | number | 0 | No | Tilt angle. The value ranges from 0 to 40, in degrees. Since: 2.0.2 |
| min-scale | number | 2 | No | Minimum zoom level. The value ranges from 2 to 20. Since: 2.0.2 |
| max-scale | number | 20 | No | Maximum zoom level. The value ranges from 2 to 20. Since: 2.0.2 |
| markers | Array<marker> | - | No | Marker. For details, see marker. |
| circles | Array<circle> | - | No | Circle. For details, see circle. |
| polyline | Array<polyline> | - | No | Polyline. For details, see polyline. |
| polygons | Array<polygon> | - | No | Polygon. For details, see polygon. Since: 1.0.18 |
| show-location | boolean | false | No | Whether to display the current location point. |
| show-compass | boolean | false | No | Whether to display the compass. Since: 1.0.18 |
| show-scale | boolean | false | No | Whether to display scale controls. Since: 2.0.2 |
| enable-rotate | boolean | false | No | Whether to support rotation. Since: 1.0.18 |
| enable-scroll | boolean | true | No | Whether to support panning. Since: 1.0.18 |
| enable-zoom | boolean | true | No | Whether to support zooming. Since: 1.0.18 |
| enable-center-scale | boolean | false | No | Whether to anchor map zooming to the component's center. true: Map zooming is anchored to the component's center. false: Map zooming is anchored to the center point of the user's gesture. Since: 1.0.19 |
| enable-zoom-controls | boolean | false | No | Whether to enable the map zooming controller. true: The map zooming controller is enabled. false: The map zooming controller is disabled. Since: 2.0.2 |
| enable-overlooking | boolean | false | No | Whether to enable tilt gestures. - When enabled, users can use two fingers to swipe up or down vertically to tilt the camera. - When disabled, users cannot tilt the camera through tilt gestures. Since: 2.0.2 |
| enable-traffic | boolean | false | No | Whether to enable real-time traffic. true: Real-time traffic is enabled. false: Real-time traffic is disabled. Since: 2.0.2 |
| include-points | Array<point> | - | No | Whether to zoom in on the map to show all given coordinates. Parameter format: [{latitude: 0, longitude: 0}, {latitude: 1, longitude: 1}] Since: 2.0.2 |
| subkey | string | - | No | Custom style ID. If the ID is invalid, the system style is used. The value cannot be dynamically updated. For details about how to obtain the ID, see step 2 in "Setting the Style ID" in Displaying a Custom Map. Since: 2.0.2 |
| setting | Object | - | No | Attribute configuration item. For details, see setting. Since: 2.0.2 |
| bindtap | event | - | No | Event triggered when the map is tapped. |
| bindmarkertap | event | - | No | Event triggered when a marker is tapped. The format is e.detail = {markerId}. |
| bindupdated | event | - | No | Event triggered when map rendering update is complete. |
| bindregionchange | event | - | No | Event triggered when the field of view changes. Since: 1.0.8 |
| bindcallouttap | event | - | No | Event triggered when the callout corresponding to the marker is tapped. The format is e.detail = {markerId}. Since: 1.0.10 |
| bindpolylinetap | event | - | No | Event triggered when a polyline on the map is tapped. The format is e.detail = {polylineId}. Since: 2.0.2 |
| bindpoitap | event | - | No | Event triggered when a POI on the map is tapped. The format is e.detail = {name, longitude, latitude}. Since: 2.0.2 |
| binderror | event | - | No | Event triggered when the map fails to be created or the map service is abnormal. The format is e.detail = {errCode, errMsg}. Since: 2.0.2 |
Return values of bindregionchange
| Name | Type | Description |
|---|---|---|
| type | string | Event type. begin: triggered when the field of view begins to change. end: triggered when the field of view finishes changing. |
| causedBy | string | Reason why the field of view changes. The value of causedBy returned in the begin phase: 1. gesture: gesture. 2. update: API call. The value of causedBy returned in the end phase: 1. drag: dragging the map. 2. scale: scaling the map. 3. update: API call. |
Error code information
| Error Code | Error Message | Description |
|---|---|---|
| 40001 | Failed to create map. | Failed to create the map. |
| 40002 | Map service unavailable. | The map service is abnormal. |
| Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| id | number | - | Yes | Marker ID. This ID will be returned when the marker tap event is called back. |
| latitude | number | - | Yes | Latitude. The value is a floating point number ranging from –90 to 90. Note: Use gcj02 for the Chinese mainland and Hong Kong and Macao regions, and use wgs84 for other regions. |
| longitude | number | - | Yes | Longitude. The value is a floating point number ranging from –180 to 180. Note: Use gcj02 for the Chinese mainland and Hong Kong and Macao regions, and use wgs84 for other regions. |
| title | string | - | No | Marker title, which is displayed when a marker is tapped. Since 1.0.10, the callout property is supported. If callout exists, title will be ignored. |
| zIndex | number | - | No | Display level. |
| iconPath | string | - | Yes | Icon path, which can be a network path, local path, or code package path. In versions earlier than ASCF 2.0.1, the image is displayed based on the original dimensions by default. In ASCF 2.0.1 and later versions, the image is displayed based on the dimensions calculated based on the current screen density by default. |
| rotate | number | 0 | No | Rotation angle in the clockwise direction. The value ranges from 0 to 360. The default value is 0. |
| alpha | number | 1 | No | Annotation alpha. The value ranges from 0 to 1. The default value is 1, indicating no transparency. |
| width | number | - | No | Actual image width by default. The unit can be rpx or px. The default unit is px. Since: 2.0.2 |
| height | number | - | No | Actual image height by default. The unit can be rpx or px. The default unit is px. Since: 2.0.2 |
| anchor | Object | - | No | Anchor of a marker icon, which corresponds to a specific pair of latitude and longitude. The default value is the midpoint of the icon bottom edge. The value format is {x, y}, where x indicates the horizontal position, with a value range of 0 to 1, and y indicates the vertical position, with a value range of 0 to 1. For example, {x: 0.5, y: 1} indicates the midpoint of the icon bottom edge. |
| clickable | boolean | true | No | Whether a marker is tappable. The default value is true. If an invalid value is passed, the default value will be used. true: yes; false: no. |
| callout | Object | - | No | Callout displayed above the marker. For details, see Callout on a Marker. Since: 1.0.10 |
| label | Array<Object> | - | No | Label added next to the marker. For details, see Label on a Marker. Since: 1.0.10 |
| customCallout | Object | - | No | Custom callout. For details, see Custom Callout on a Marker. Since: 1.0.22 |
| Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| content | string | - | No | Text. |
| padding | number|string | 0 | No | Text padding, in rpx or px (default). |
| bgColor | string | #FFFFFF (white) | No | Background color. |
| color | string | #000000 (black) | No | Text color. |
| fontSize | number|string | 15 | No | Text size, in rpx or px (default). |
| weight | string | Normal | No | Font weight. Valid values: Lighter: thinner than Normal Normal: standard font weight Regular: same as Normal Medium: slightly thicker than Normal Bold: significantly thicker Bolder: extremely thick |
| fontStyle | string | Normal | No | Title font style. Valid values: Normal: standard font style Italic: italic |
| textAlign | string | center | No | Text alignment mode. Valid values: Start: left-aligned End: right-aligned Center: centered |
| borderWidth | number|string | 0 | No | Border width, in rpx or px (default). |
| borderColor | string | #FFFFFF (white) | No | Border color. |
| borderRadius | number|string | 0 | No | Border radius, in rpx or px (default). |
| anchorX | number | 0 | No | Horizontal offset. A positive number indicates the right direction. The unit can be rpx or px (default). Since: 2.0.2 |
| anchorY | number | 0 | No | Vertical offset. A positive number indicates the downward direction. The unit can be rpx or px (default). Since: 2.0.2 |
| Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| content | string | - | Yes | Text. |
| color | string | #000000 (black) | No | Title font color. |
| fontSize | number|string | 15 | No | Font size of the title, in rpx or px (default). |
| strokeColor | string | #FFFFFF (white) | No | Title stroke color. |
| strokeWidth | number|string | 2 | No | Stroke width of the title. The value must be greater than 0 and less than or equal to 10, in rpx or px (default). |
| fontStyle | string | REGULAR | No | Title font style. Valid values: REGULAR: normal BOLD: bold ITALIC: italic BOLD_ITALIC: bold italic MEDIUM: medium MEDIUM_ITALIC: medium italic |
| anchorX | number | 0 | No | Horizontal offset. A positive number indicates the right direction. The unit can be rpx or px (default). Since: 2.0.2 |
| anchorY | number | 0 | No | Vertical offset. A positive number indicates the downward direction. The unit can be rpx or px (default). Since: 2.0.2 |
| Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| display | string | BYCLICK | No | Callout display status. Valid values: BYCLICK: displayed upon a tap. ALWAYS: always displayed. |
| anchorX | number | 0 | No | Horizontal offset. A positive number indicates the right direction. The unit can be rpx or px (default). |
| anchorY | number | 0 | No | Vertical offset. A positive number indicates the downward direction. The unit can be rpx or px (default). |
Add a slot named callout to the map component. The cover-view component inside the slot is bound to the marker through the marker-id attribute. When a marker is created, the content displayed in the cover-view component is displayed as a callout above the marker.
<map>
<cover-view slot="callout">
<cover-view marker-id="1"></cover-view>
<cover-view marker-id="2"></cover-view>
</cover-view>
</map> Constraints:
If customCallout exists, the callout and title attributes are ignored.
Custom callouts can be customized using view, image, cover-view, and cover-image.
It is recommended that the number of custom callouts do not exceed 50.
The gesture events (such as bindtap and bindtouchstart) bound to nodes in the callout slot do not take effect. To handle callout tap events, use the bindcallouttap attribute of the map component.
| Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| points | array | - | Yes | Longitude and latitude array, in the format of [{latitude: 0, longitude: 0}]. |
| strokeWidth | number|string | 1 | No | Stroke width, in rpx or px. The default value is vp. Note: If no pixel unit is specified for a string, the default unit is vp. For example, '10' is equivalent to 10 vp. If a number is passed, the default unit is vp. |
| strokeColor | string | #0A59F7CC | No | Stroke color. |
| fillColor | string | #0A59F71A | No | Fill color. |
| zIndex | number | - | No | Z-axis index of the polygon. |
| Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| latitude | number | - | Yes | Latitude. The value is a floating-point number ranging from –90 to 90. Note: Use gcj02 for the Chinese mainland and Hong Kong and Macao regions, and use wgs84 for other regions. |
| longitude | number | - | Yes | Longitude. The value is a floating-point number ranging from –180 to 180. Note: Use gcj02 for the Chinese mainland and Hong Kong and Macao regions, and use wgs84 for other regions. |
| color | string | #000000ff | No | Stroke color. |
| fillColor | string | #00000000 | No | Fill color. |
| radius | number | - | Yes | Radius of a circle, in meters. |
| strokeWidth | number|string | 1 | No | Stroke width, in rpx or px. The default value is vp. Note: If no pixel unit is specified for a string, the default unit is vp. For example, '10' is equivalent to 10 vp. If a number is passed, the default unit is vp. |
| zIndex | number | - | No | Z-axis index, which is used to describe element overlapping relationships. |
| Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| id | number | - | No | Polyline ID (used when a MapContext.eraseLines polyline is removed). Since: 2.0.2 |
| points | array | - | Yes | Longitude-latitude array, which is in the [{latitude: 0, longitude: 0}] format. Note: Use gcj02 for the Chinese mainland and Hong Kong and Macao regions, and use wgs84 for other regions. |
| width | number|string | 1 | No | Width of a polyline, in rpx or px. The default unit is vp. Note: If no pixel unit is specified for a string, the default unit is vp. For example, '10' is equivalent to 10 vp. If a number is passed, the default unit is vp. |
| color | string | "#000000ff" | No | Color of a polyline. |
| dottedLine | boolean | false | No | Whether the polyline is a dotted line. |
| arrowLine | boolean | false | No | Whether the polyline is with an arrow. Since: 2.0.2 |
| arrowIconPath | string | - | No | Icon path of the arrow, which can be a network path, local path, or code package path. This parameter takes effect only when arrowLine is set to true. Since: 2.0.2 |
| zIndex | number | - | No | Z coordinate. |
This parameter is used to configure map attributes in a unified manner. This solves the problem that animation attributes such as rotate and skew cannot take effect at the same time when they are set separately.
this.setData({
// Only the attributes that are set take effect, and other attributes are not affected.
setting: {
skew: 20,
rotate: 90,
showLocation: true,
enableOverlooking: true,
enableTraffic: true
}
}); Example:
HXML file:
<view>
<map
id="map"
style="width: 100%; height: 100vh"
latitude="{{ latitude }}"
longitude="{{ longitude }}"
markers="{{ markers }}"
scale="{{ scale }}"
show-location="{{ showLocation }}"
bindmarkertap="bindmarkertap"
bindtap="bindtap"
bindupdated="bindupdated"/>
</view> JavaScript file:
Page({
data: {
showLocation: true,
latitude: 23.106574,
longitude: 113.324587,
scale: 10,
markers: [
{
id: 0,
latitude: 23.13065, // Latitude
longitude: 113.3274, // Longitude
title: 'I am a marker', // Marker name
zIndex: 1,
iconPath: 'xxx/xxx.png', // Icon to be displayed
rotate: 10, // Rotation angle
alpha: 1, // Alpha
anchor: { x: 0.5, y: 1 },
clickable: true
}
]
},
bindmarkertap(e) {
console.info('markertap=', e);
},
bindtap(e) {
console.info('bindtap=', e);
},
bindupdated(e) {
console.info('bindupdated=', e);
}
}); Scenario 2: Setting a callout on the marker
HXML file:
<view>
<map
id="map"
style="width: 100%; height: 100vh"
latitude="{{ latitude }}"
longitude="{{ longitude }}"
scale="{{ scale }}"
markers="{{ markers }}"
bindcallouttap="bindcallouttap"/>
</view> JavaScript file:
Page({
data: {
latitude: 23.106574,
longitude: 113.324587,
scale: 10,
markers: [
{
id: 0,
latitude: 23.13065, // Latitude
longitude: 113.3274, // Longitude
zIndex: 1,
iconPath: 'xxx/xxx.png', // Icon to be displayed
clickable: true,
callout: {
content: 'Callout title',
padding: 5,
bgColor: '#FFFFFF',
color: '#000000',
fontSize: 15,
weight: 'Normal',
fontStyle: 'Normal',
textAlign: 'Center',
borderWidth: 2,
borderColor: '#FF0000',
borderRadius: 10
}
}
]
},
bindcallouttap(e) {
console.info('bindcallouttap=', e);
}
}); Scenario 3: Setting a label next to the marker
HXML file:
<view>
<map
id="map"
style="width: 100%; height: 100vh"
latitude="{{ latitude }}"
longitude="{{ longitude }}"
scale="{{ scale }}"
markers="{{ markers }}"/>
</view> JavaScript file:
Page({
data: {
latitude: 23.106574,
longitude: 113.324587,
scale: 10,
markers: [
{
id: 0,
latitude: 23.13065, // Latitude
longitude: 113.3274, // Longitude
zIndex: 1,
iconPath: 'xxx/xxx.png', // Icon to be displayed
clickable: true,
label: [
{
content: 'Label title',
color: '#000000',
fontSize: 15,
strokeColor: '#FF0000',
strokeWidth: 10,
fontStyle: 'REGULAR',
}
]
}
]
}
}); HXML file:
<view>
<map
id="map"
style="width: 100%; height: 100vh"
latitude="{{ latitude }}"
longitude="{{ longitude }}"
scale="{{ scale }}"
circles="{{ circles }}"/>
</view> JavaScript file:
Page({
data: {
latitude: 23.106574,
longitude: 113.324587,
scale: 12,
circles: [{
latitude: 23.106574,
longitude: 113.324587,
color: '#000000ff',
fillColor: '#ff0000',
radius: 1000,
strokeWidth: 10,
zIndex: 1
}]
}
}); HXML file:
<view>
<map
id="map"
style="width: 100%; height: 100vh"
latitude="{{ latitude }}"
longitude="{{ longitude }}"
scale="{{ scale }}"
polygons="{{ polygons }}"/>
</view> JavaScript file:
Page({
data: {
latitude: 23.106574,
longitude: 113.324587,
scale: 12,
polygons: [{
points: [
{ longitude: 113.314587, latitude: 23.116574 },
{ longitude: 113.314587, latitude: 23.096574 },
{ longitude: 113.334587, latitude: 23.096574 },
{ longitude: 113.334587, latitude: 23.116574 }
],
strokeWidth: 10,
strokeColor: '#000000',
fillColor: '#FF0000',
zIndex: 10
}]
}
}); Scenario 6: Setting a polyline
HXML file:
<view>
<map
id="map"
style="width: 100%; height: 100vh;"
latitude="{{ latitude }}"
longitude="{{ longitude }}"
polyline="{{ polyline }}"
scale="{{ scale }}"
show-location="{{ showLocation }}"/>
</view> JavaScript file:
Page({
data: {
showLocation: true,
latitude: 30.490487597079223,
longitude: 114.53563002817789,
scale: 12,
polyline: [{
points: [
{ latitude: 30.490487597079223, longitude: 114.53563002817789 },
{ latitude: 30.490559597079223, longitude: 114.55880402817789 }
],
color: '#FF0000',
zIndex: 0
}]
}
}); Scenario 7: Setting a custom callout for a marker
HXML file:
<view>
<map
id="map"
style="width: 100%; height: 100vh"
latitude="{{ latitude }}"
longitude="{{ longitude }}"
scale="{{ scale }}"
markers="{{ markers }}"
bindcallouttap="bindcallouttap">
<cover-view slot="callout">
<cover-view class="customCallout" marker-id="1">
marker-1
</cover-view>
</cover-view>
</map>
</view> JavaScript file:
Page({
data: {
latitude: 23.106574,
longitude: 113.324587,
scale: 10,
markers: [
{
id: 1,
latitude: 23.095994,
longitude: 113.325520,
customCallout: {
anchorY: 0,
anchorX: 0,
display: 'ALWAYS'
}
}
]
},
bindcallouttap(e) {
console.info('Tap the marker callout. Marker ID:', e.detail.markerId);
}
}); CSS file:
.customCallout {
box-sizing: border-box;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 15px;
width: 150px;
height: 30px;
display: inline-flex;
padding: 5px 20px;
justify-content: center;
align-items: center;
}