# uni.createMapContext(mapId, component?)

创建并返回 map 上下文 mapContext 对象

参考:Map组件

# createMapContext 兼容性

Web 微信小程序 Android iOS HarmonyOS 系统版本 HarmonyOS
4.0 4.41 4.31 4.31 4.61

# 参数

名称 类型 必填 默认值 兼容性 描述
mapId string
component ComponentPublicInstance

# 返回值

类型 描述 必备
MapContext map组件上下文对象

# MapContext 的方法

# getCenterLocation(options : MapContextGetCenterLocationOptions) : void;

getCenterLocation 获取当前地图中心的经纬度,返回的是 gcj02 坐标系,可以用于 uni.openLocation

# getCenterLocation 兼容性
Android iOS HarmonyOS 系统版本 HarmonyOS
4.31 4.31 4.61
# 参数
名称 类型 必填 默认值 兼容性 描述
options MapContextGetCenterLocationOptions
名称 类型 必备 默认值 兼容性 描述
success (result: LocationObject) => void 接口调用成功的回调函数
fail (result: MapContextFail) => void null 接口调用失败的回调函数
complete (result: any) => void null 接口调用结束的回调函数(调用成功、失败都会执行)
# LocationObject 的属性值
名称 类型 必备 默认值 兼容性 描述
latitude number
纬度,浮点数,范围为-90~90,负数表示南纬
longitude number
经度,范围为-180~180,负数表示西经
# MapContextFail 的属性值
名称 类型 必备 默认值 兼容性 描述
errCode number 错误码
合法值 描述
500001 获取当前地图中心的经纬度失败
500002 未找到当前定位位置
500003 未找到marker
500004 创建自定义图片图层失败
500005 未找到自定义图层id
500006 网络图片加载失败
500012 地图内部错误
errSubject string 统一错误主题(模块)名称
data any 错误信息中包含的数据
cause Error 源错误信息,可以包含多个错误,详见SourceError
errMsg string

# moveToLocation(options : MapContextMoveToLocationOptions) : void;

moveToLocation 将地图中心移动到当前定位点,需要配合map组件的show-location使用

# moveToLocation 兼容性
Android iOS HarmonyOS 系统版本 HarmonyOS
4.31 4.31 4.61
# 参数
名称 类型 必填 默认值 兼容性 描述
options MapContextMoveToLocationOptions
名称 类型 必备 默认值 兼容性 描述
latitude number null
纬度,浮点数,范围为-90~90,负数表示南纬
longitude number null
经度,范围为-180~180,负数表示西经
success (result: any) => void null 接口调用成功的回调函数
fail (result: MapContextFail) => void null 接口调用失败的回调函数
complete (result: any) => void null 接口调用结束的回调函数(调用成功、失败都会执行)

# translateMarker(options : MapContextTranslateMarkerOptions) : void;

translateMarker 平移marker,带动画

# translateMarker 兼容性
Android iOS HarmonyOS 系统版本 HarmonyOS
4.31 4.31 4.61
# 参数
名称 类型 必填 默认值 兼容性 描述
options MapContextTranslateMarkerOptions
名称 类型 必备 默认值 兼容性 描述
markerId number
指定marker
destination LocationObject
指定marker移动到的目标点
autoRotate boolean
移动过程中是否自动旋转marker
rotate number 0
marker的旋转角度
moveWithRotate boolean false
平移和旋转同时进行
duration number 1000
动画持续时长,平移与旋转分别计算
success (result: any) => void null 接口调用成功的回调函数
fail (result: MapContextFail) => void null 接口调用失败的回调函数
complete (result: any) => void null 接口调用结束的回调函数(调用成功、失败都会执行)

# includePoints(options : MapContextIncludePointsOptions) : void;

includePoints 缩放视野展示所有经纬度

# includePoints 兼容性
Android iOS HarmonyOS 系统版本 HarmonyOS
4.31 4.31 4.61
# 参数
名称 类型 必填 默认值 兼容性 描述
options MapContextIncludePointsOptions
名称 类型 必备 默认值 兼容性 描述
points Array<LocationObject>
要显示在可视区域内的坐标点列表,[{latitude, longitude}]
success (result: any) => void null 接口调用成功的回调函数
fail (result: MapContextFail) => void null 接口调用失败的回调函数
complete (result: any) => void null 接口调用结束的回调函数(调用成功、失败都会执行)

# getRegion(options : MapContextGetRegionOptions) : void;

getRegion 获取当前地图的视野范围

# getRegion 兼容性
Android iOS HarmonyOS 系统版本 HarmonyOS
4.31 4.31 4.61
# 参数
名称 类型 必填 默认值 兼容性 描述
options MapContextGetRegionOptions
名称 类型 必备 默认值 兼容性 描述
success (result: MapContextGetRegionResult) => void null 接口调用成功的回调函数
fail (result: MapContextFail) => void null 接口调用失败的回调函数
complete (result: any) => void null 接口调用结束的回调函数(调用成功、失败都会执行)
# MapContextGetRegionResult 的属性值
名称 类型 必备 默认值 兼容性 描述
southwest LocationObject
西南角的经纬度
northeast LocationObject
东北角的经纬度

# getScale(options : MapContextGetScaleOptions) : void;

getScale 获取当前地图的缩放级别

# getScale 兼容性
Android iOS HarmonyOS 系统版本 HarmonyOS
4.31 4.31 4.61
# 参数
名称 类型 必填 默认值 兼容性 描述
options MapContextGetScaleOptions
名称 类型 必备 默认值 兼容性 描述
success (result: MapContextGetScaleResult) => void null 接口调用成功的回调函数
fail (result: MapContextFail) => void null 接口调用失败的回调函数
complete (result: any) => void null 接口调用结束的回调函数(调用成功、失败都会执行)
# MapContextGetScaleResult 的属性值
名称 类型 必备 默认值 兼容性 描述
scale number
地图缩放级别

# addGroundOverlay(options : MapContextAddGroundOverlayOptions) : void;

addGroundOverlay 创建自定义图片图层,图片会随着地图缩放而缩放

# addGroundOverlay 兼容性
Android iOS HarmonyOS
4.31 4.31 x
# 参数
名称 类型 必填 默认值 兼容性 描述
options MapContextAddGroundOverlayOptions
名称 类型 必备 默认值 兼容性 描述
id string
图片图层 id
src string
图片路径,支持网络图片、临时路径、代码包路径
bounds Bounds
图片覆盖的经纬度范围
名称 类型 必备 默认值 兼容性 描述
southwest LocationObject
西南角的经纬度
northeast LocationObject
东北角的经纬度
visible boolean false
是否可见
zIndex number 0
图层绘制顺序
opacity number 1
图层透明度
success (result: any) => void null 接口调用成功的回调函数
fail (result: MapContextFail) => void null 接口调用失败的回调函数
complete (result: any) => void null 接口调用结束的回调函数(调用成功、失败都会执行)

# addMarkers(options : MapContextAddMarkersOptions) : void;

addMarkers 添加 marker

# addMarkers 兼容性
Android iOS HarmonyOS 系统版本 HarmonyOS
4.31 4.31 4.61
# 参数
名称 类型 必填 默认值 兼容性 描述
options MapContextAddMarkersOptions
名称 类型 必备 默认值 兼容性 描述
markers Array<Marker>
同传入 map 组件的 marker 属性
名称 类型 必备 默认值 兼容性 描述
id number
标记点id,marker点击事件回调会返回此id。建议为每个marker设置上Number类型id,保证更新marker时有更好的性能。最大限制9位数
latitude number
纬度,浮点数,范围 -90 ~ 90
longitude number
经度,浮点数,范围 -180 ~ 180
iconPath string
显示的图标,项目目录下的图片路径,支持相对路径写法,以'/'开头则表示相对小程序根目录;也支持临时路径
title string null
标注点名,点击时显示,callout存在时将被忽略
rotate number 0
旋转角度,顺时针旋转的角度,范围 0 ~ 360
alpha number 1
标注的透明度,范围 0 ~ 1
width number 默认为图片实际宽度
标注图标宽度
height number 默认为图片实际高度
标注图标高度
ariaLabel string null
无障碍访问,(属性)元素的额外描述
anchor Anchor [0.5, 1]
经纬度在标注图标的锚点,默认底边中点 {x, y},x表示横向(0-1),y表示竖向(0-1)。{x: .5, y: 1} 表示底边中点
名称 类型 必备 默认值 兼容性 描述
x number
y number
callout MapMarkerCallout null
自定义标记点上方的气泡窗口
名称 类型 必备 默认值 兼容性 描述
content string ""
文本
color string.ColorString "black"
文本颜色
fontSize number null
文字大小
borderRadius number 0
边框圆角
borderWidth number 0
边框宽度
borderColor string.ColorString "transparent"
边框颜色
bgColor string.ColorString "#fff"
背景色
padding number 0
文本边缘留白
display string "BYCLICK"
'BYCLICK':点击显示; 'ALWAYS':常显
合法值 描述
BYCLICK
ALWAYS
textAlign string "left"
文本对齐方式。
合法值 描述
left
center
right
anchorX number 0.5
横向偏移量,向右为正数
anchorY number 1
纵向偏移量,向下为正数
label MapMarkerLabel null
为标记点旁边增加标签
名称 类型 必备 默认值 兼容性 描述
content string 1
文本
color string 1
文本颜色
fontSize number 1
文字大小
x number 1
label的坐标,原点是 marker 对应的经纬度
y number 1
label的坐标,原点是 marker 对应的经纬度
anchorX number 1
label的坐标,原点是 marker 对应的经纬度
anchorY number 1
label的坐标,原点是 marker 对应的经纬度
borderWidth number 1
边框宽度
borderColor string 1
边框颜色
borderRadius number 1
边框圆角
bgColor string 1
背景色
padding number 1
文本边缘留白
textAlign string 1
文本对齐方式。
合法值 描述
left
center
right
ariaLabel string 1
无障碍访问,(属性)元素的额外描述
clusterId number null
自定义点聚合簇效果时使用
customCallout MapMarkerCallout null
自定义气泡窗口
joinCluster boolean null
是否参与点聚合
clear boolean
是否先清空地图上所有 marker
success (result: any) => void null 接口调用成功的回调函数
fail (result: MapContextFail) => void null 接口调用失败的回调函数
complete (result: any) => void null 接口调用结束的回调函数(调用成功、失败都会执行)

# moveAlong(options : MapContextMoveAlongOptions) : void;

moveAlong 沿指定路径移动 marker,用于轨迹回放等场景。动画完成时触发回调事件,若动画进行中,对同一 marker 再次调用 moveAlong 方法,前一次的动画将被打断。

# moveAlong 兼容性
Android iOS HarmonyOS 系统版本 HarmonyOS
4.31 4.31 4.61
# 参数
名称 类型 必填 默认值 兼容性 描述
options MapContextMoveAlongOptions
名称 类型 必备 默认值 兼容性 描述
markerId number
指定marker
path Array<LocationObject>
移动路径的坐标串,坐标点格式 {longitude, latitude}
duration number 1000
平滑移动的时间
success (result: any) => void null 接口调用成功的回调函数
fail (result: MapContextFail) => void null 接口调用失败的回调函数
complete (result: any) => void null 接口调用结束的回调函数(调用成功、失败都会执行)

# removeGroundOverlay(options : MapContextRemoveGroundOverlayOptions) : void;

removeGroundOverlay 移除自定义图片图层

# removeGroundOverlay 兼容性
Android iOS HarmonyOS
4.31 4.31 x
# 参数
名称 类型 必填 默认值 兼容性 描述
options MapContextRemoveGroundOverlayOptions
名称 类型 必备 默认值 兼容性 描述
id string
图片图层 id
success (result: any) => void null 接口调用成功的回调函数
fail (result: MapContextFail) => void null 接口调用失败的回调函数
complete (result: any) => void null 接口调用结束的回调函数(调用成功、失败都会执行)

# removeMarkers(options : MapContextRemoveMarkersOptions) : void;

removeMarkers 移除 marker

# removeMarkers 兼容性
Android iOS HarmonyOS 系统版本 HarmonyOS
4.31 4.31 4.61
# 参数
名称 类型 必填 默认值 兼容性 描述
options MapContextRemoveMarkersOptions
名称 类型 必备 默认值 兼容性 描述
markerIds Array<number>
要被删除的marker的id属性组成的数组
success (result: any) => void null 接口调用成功的回调函数
fail (result: MapContextFail) => void null 接口调用失败的回调函数
complete (result: any) => void null 接口调用结束的回调函数(调用成功、失败都会执行)

# updateGroundOverlay(options : MapContextUpdateGroundOverlayOptions) : void;

updateGroundOverlay 更新自定义图片图层。

# updateGroundOverlay 兼容性
Android iOS HarmonyOS
4.31 4.31 x
# 参数
名称 类型 必填 默认值 兼容性 描述
options MapContextUpdateGroundOverlayOptions
名称 类型 必备 默认值 兼容性 描述
id string
图片图层 id
src string
图片路径,支持网络图片、临时路径、代码包路径
bounds Bounds
图片覆盖的经纬度范围
visible boolean true
是否可见
zIndex number 0
图层绘制顺序
opacity number 1
图层透明度
success (result: any) => void null 接口调用成功的回调函数
fail (result: MapContextFail) => void null 接口调用失败的回调函数
complete (result: any) => void null 接口调用结束的回调函数(调用成功、失败都会执行)

# 参见