微信小程序API 地圖·MapContext對象

2022-04-28 15:17 更新

MapContext 實(shí)例,可通過 wx.createMapContext 獲取。

MapContext 通過 id 跟一個(gè) map 組件綁定,操作對應(yīng)的 map 組件。

方法

MapContext.getCenterLocation()

獲取當(dāng)前地圖中心的經(jīng)緯度。返回的是 gcj02 坐標(biāo)系,可以用于 wx.openLocation()

MapContext.moveToLocation(Object object)

將地圖中心移置當(dāng)前定位點(diǎn),此時(shí)需設(shè)置地圖組件 show-locationtrue。2.8.0 起支持將地圖中心移動(dòng)到指定位置。

MapContext.translateMarker(Object object)

平移marker,帶動(dòng)畫

MapContext.includePoints(Object object)

縮放視野展示所有經(jīng)緯度

MapContext.getRegion()

獲取當(dāng)前地圖的視野范圍

MapContext.getRotate()

獲取當(dāng)前地圖的旋轉(zhuǎn)角

MapContext.getSkew()

獲取當(dāng)前地圖的傾斜角

MapContext.getScale()

獲取當(dāng)前地圖的縮放級別

MapContext.setCenterOffset(Object object)

設(shè)置地圖中心點(diǎn)偏移,向后向下為增長,屏幕比例范圍(0.25~0.75),默認(rèn)偏移為[0.5, 0.5]

MapContext.removeCustomLayer(Object object)

移除個(gè)性化圖層。

MapContext.addCustomLayer(Object object)

添加個(gè)性化圖層。

示例代碼

<!-- map.wxml -->
<map id="myMap" show-location />

<button type="primary" bindtap="getCenterLocation">獲取位置</button>
<button type="primary" bindtap="moveToLocation">移動(dòng)位置</button>
<button type="primary" bindtap="translateMarker">移動(dòng)標(biāo)注</button>
<button type="primary" bindtap="includePoints">縮放視野展示所有經(jīng)緯度</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,
      }]
    })
  }
})


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號