微信小程序开发 专题
专题目录
您的位置:微信小程序开发 > 微信小程序开发专题 > 微信小程序API 地图组件控制
微信小程序API 地图组件控制
作者:--    发布时间:2019-11-20

wx.createmapcontext(mapid)


创建并返回 map 上下文 mapcontext 对象。在自定义组件下,第二个参数传入组件实例this,以操作组件内 <map/> 组件


mapcontext

mapcontext通过 mapid 跟一个<map/>组件绑定,通过它可以操作对应的<map/>组件。

mapcontext 对象的方法列表

方法参数说明最低版本
getcenterlocationobject获取当前地图中心的经纬度,返回的是 gcj02 坐标系,可以用于 wx.openlocation 
movetolocation将地图中心移动到当前定位点,需要配合map组件的show-location使用 
translatemarkerobject平移marker,带动画1.2.0
includepointsobject缩放视野展示所有经纬度1.2.0
getregionobject获取当前地图的视野范围1.4.0
getscaleobject获取当前地图的缩放级别1.4.0

getcenterlocation 的 object 参数列表

参数类型必填说明
successfunction接口调用成功的回调函数 ,res = { longitude: "经度", latitude: "纬度"}
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

translatemarker 的 object 参数列表

参数类型必填说明
markeridnumber指定marker
destinationobject指定marker移动到的目标点
autorotateboolean移动过程中是否自动旋转marker
rotatenumbermarker的旋转角度
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.wxml -->
<map id="mymap" show-location />

<button type="primary" bindtap="getcenterlocation">获取位置</button>
<button type="primary" bindtap="movetolocation">移动位置</button>
<button type="primary" bindtap="translatemarker">移动标注</button>
<button type="primary" bindtap="includepoints">缩放视野展示所有经纬度</button>
// 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,
      }]
    })
  }
})


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册