wx.createMapContext(mapId)
创建并返回 map 上下文mapContext对象。在自定义组件下,第二个参数传入组件实例this,以操作组件内组件
mapContext
mapContext通过 mapId 跟一个组件绑定,通过它可以操作对应的组件。
mapContext 对象的方法列表方法参数说明最低版本getCenterLocationOBJECT获取当前地图中心的经纬度,返回的是 gcj02 坐标系,可以用于wx.openLocation
moveToLocation无将地图中心移动到当前定位点,需要配合map组件的show-location使用
translateMarkerOBJECT平移marker,带动画
includePointsOBJECT缩放视野展示所有经纬度
getRegionOBJECT获取当前地图的视野范围
getScaleOBJECT获取当前地图的缩放级别
getCenterLocation 的 OBJECT 参数列表参数类型必填说明successFunction否接口调用成功的回调函数 ,res = { longitude: "经度", latitude: "纬度"}
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)
translateMarker 的 OBJECT 参数列表参数类型必填说明markerIdNumber是指定marker
destinationObject是指定marker移动到的目标点
autoRotateBoolean是移动过程中是否自动旋转marker
rotateNumber是marker的旋转角度
durationNumber否动画持续时长,默认值1000ms,平移与旋转分别计算
animationEndFunction否动画结束回调函数
failFunction否接口调用失败的回调函数
includePoints 的 OBJECT 参数列表参数类型必填说明pointsArray是要显示在可视区域内的坐标点列表,[{latitude, longitude}]
paddingArray否坐标点形成的矩形边缘到地图边缘的距离,单位像素。格式为[上,右,下,左],安卓上只能识别数组第一项,上下左右的padding一致。开发者工具暂不支持padding参数。
getRegion 的 OBJECT 参数列表参数类型必填说明successFunction否接口调用成功的回调函数,res = {southwest, northeast},西南角与东北角的经纬度
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)
getScale 的 OBJECT 参数列表参数类型必填说明successFunction否接口调用成功的回调函数,res = {scale}
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)
示例代码:
获取位置
移动位置
移动标注
缩放视野展示所有经纬度// map.js
Page({
onReady: function (e) {
// 使用 wx.createMapContext 获取 map 上下文
this.mapCtx = wx.createMapContext('myMap')
},
getCenterLocation: function () {
this.mapCtx.getCenterLocation({
success: function(res){
console.log(res.longitude)
console.log(res.latitude)
}
})
},
moveToLocation: function () {
this.mapCtx.moveToLocation()
},
translateMarker: function() {
this.mapCtx.translateMarker({
markerId: 0,
autoRotate: true,
duration: 1000,
destination: {
latitude:23.10229,
longitude:113.3345211,
},
animationEnd() {
console.log('animation end')
}
})
},
includePoints: function() {
this.mapCtx.includePoints({
padding: [10],
points: [{
latitude:23.10229,
longitude:113.3345211,
}, {
latitude:23.00229,
longitude:113.3345211,
}]
})
}
})