小程序开发API之地图wx.createMapContext及MapContext 实例

版权声明:欢迎转载,可Chat交流,写博不易请标明出处: https://blog.csdn.net/JackJia2015/article/details/86700861

效果展示


在这里插入图片描述

wx.createMapContext(string mapId, Object this)

创建 map 上下文 MapContext 对象。
参数 string mapId
组件的 id
Object this
在自定义组件下,当前组件实例的this,以操作组件内
组件
返回值
MapContext

MapContext

MapContext 实例,可通过 wx.createMapContext 获取。
mapContext 通过 id 跟一个 组件绑定,操作对应的 组件。

方法

MapContext.getCenterLocation()

获取当前地图中心的经纬度。返回的是 gcj02 坐标系,可以用于 wx.openLocation()
getCenterLocation参数在这里插入图片描述

object.success 回调函数参数Object res在这里插入图片描述

MapContext.moveToLocation()

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

MapContext.translateMarker(Object object)

平移marker,带动画
translateMarker参数在这里插入图片描述

object.destination 的结构在这里插入图片描述

MapContext.includePoints(Object object)

缩放视野展示所有经纬度
includePoints参数在这里插入图片描述

object.points 的结构在这里插入图片描述

MapContext.getRegion()

获取当前地图的视野范围
getRegion参数在这里插入图片描述

object.success 回调函数参数Object res在这里插入图片描述

MapContext.getScale()

获取当前地图的缩放级别
getScale参数在这里插入图片描述

object.success 回调函数参数Object res在这里插入图片描述

示例:
效果展示


在这里插入图片描述

代码
index.wxml

<map 
  id="myMap" 
  style="width: 710rpx; height: 250px;" 
  latitude="{{latitude}}" 
  longitude="{{longitude}}" 
  markers="{{markers}}" 
  show-location>
</map>
<button bindtap="getCenterLocation" type="primary">获取当前地图中心的经纬度</button>
<button bindtap="moveToLocation"  type="primary">将地图中心移动到当前定位点</button>
<button bindtap="translateMarker" type="primary">平移marker,带动画</button>
<button bindtap="includePoints" type="primary">缩放视野展示所有经纬度</button>
<button bindtap="scaleClick" type="primary">获取当前地图的缩放级别</button>
<button bindtap="getRegionClick" type="primary">获取当前地图的视野范围</button>

index.wxss

map{
  margin: 20rpx;
}
button{
  margin: 20rpx;
}

index.js

Page({
  data: {
    latitude: 23.099994,
    longitude: 113.324520,
    markers: [{
      id: 1,
      latitude: 23.099994,
      longitude: 113.324520,
      name: '腾讯'
    }],
  },
  onReady: function (e) {
    //创建 map 上下文 MapContext 对象。
    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()
  },
  //平移marker,带动画
  translateMarker: function() {
    this.mapCtx.translateMarker({
      markerId: 1,
      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,
      }]
    })
  },
  //获取当前地图的缩放级别
  scaleClick: function(){
    this.mapCtx.getScale({
      success: function (res) {
        console.log(res.scale)
      }
    })
  },
  //获取当前地图的视野范围
  getRegionClick: function () {
    this.mapCtx.getRegion({
      success: function (res) {
        console.log(res.southwest)
        console.log(res.northeast)
      }
    })
  }
})





猜你喜欢

转载自blog.csdn.net/JackJia2015/article/details/86700861