MapContext

MapContext 实例,可通过 wx.createMapContext 获取。

MapContext 通过 id 跟一个 map 组件绑定,操作对应的 map 组件。

方法

MapContext.getCenterLocation()

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

MapContext.moveToLocation(Object object)

将地图中心移置当前定位点,此时需设置地图组件 show-location 为true。2.8.0 起支持将地图中心移动到指定位置。

MapContext.translateMarker(Object object)

平移marker,带动画

MapContext.includePoints(Object object)

缩放视野展示所有经纬度

MapContext.getRegion()

获取当前地图的视野范围

MapContext.getRotate()

获取当前地图的旋转角

MapContext.getSkew()

获取当前地图的倾斜角

MapContext.getScale()

获取当前地图的缩放级别

示例代码

在开发者工具中预览效果

  1. <!-- map.wxml -->
  2. <map id="myMap" show-location />
  3. <button type="primary" bindtap="getCenterLocation">获取位置</button>
  4. <button type="primary" bindtap="moveToLocation">移动位置</button>
  5. <button type="primary" bindtap="translateMarker">移动标注</button>
  6. <button type="primary" bindtap="includePoints">缩放视野展示所有经纬度</button>
  1. // map.js
  2. Page({
  3. onReady: function (e) {
  4. // 使用 wx.createMapContext 获取 map 上下文
  5. this.mapCtx = wx.createMapContext('myMap')
  6. },
  7. getCenterLocation: function () {
  8. this.mapCtx.getCenterLocation({
  9. success: function(res){
  10. console.log(res.longitude)
  11. console.log(res.latitude)
  12. }
  13. })
  14. },
  15. moveToLocation: function () {
  16. this.mapCtx.moveToLocation()
  17. },
  18. translateMarker: function() {
  19. this.mapCtx.translateMarker({
  20. markerId: 0,
  21. autoRotate: true,
  22. duration: 1000,
  23. destination: {
  24. latitude:23.10229,
  25. longitude:113.3345211,
  26. },
  27. animationEnd() {
  28. console.log('animation end')
  29. }
  30. })
  31. },
  32. includePoints: function() {
  33. this.mapCtx.includePoints({
  34. padding: [10],
  35. points: [{
  36. latitude:23.10229,
  37. longitude:113.3345211,
  38. }, {
  39. latitude:23.00229,
  40. longitude:113.3345211,
  41. }]
  42. })
  43. }
  44. })