注冊(cè)

微信小程序API地圖組件控制,創(chuàng)建并返回map上下文mapContext對(duì)象

2020-09-27
導(dǎo)讀:wx.createMapContext(mapId) 創(chuàng)建并返回 map 上下文 mapContext 對(duì)象。在自定義組件下,第二個(gè)參數(shù)傳入組件實(shí)例this,以操作組件內(nèi) map/ 組件 mapContext mapContext 通過(guò) mapId 跟一個(gè) map/ 組件綁定,通過(guò)...

wx.createMapContext(mapId)

微信小程序API地圖組件控制,創(chuàng)建并返回map上下文mapContext對(duì)象


創(chuàng)建并返回 map 上下文 mapContext 對(duì)象。在自定義組件下,第二個(gè)參數(shù)傳入組件實(shí)例this,以操作組件內(nèi) <map/> 組件

 

mapContext

mapContext通過(guò) mapId 跟一個(gè)<map/>組件綁定,通過(guò)它可以操作對(duì)應(yīng)的<map/>組件。

mapContext 對(duì)象的方法列表

方法 參數(shù) 說(shuō)明 最低版本
getCenterLocation OBJECT 獲取當(dāng)前地圖中心的經(jīng)緯度,返回的是 gcj02 坐標(biāo)系,可以用于 wx.openLocation  
moveToLocation 無(wú) 將地圖中心移動(dòng)到當(dāng)前定位點(diǎn),需要配合map組件的show-location使用  
translateMarker OBJECT 平移marker,帶動(dòng)畫(huà) 1.2.0
includePoints OBJECT 縮放視野展示所有經(jīng)緯度 1.2.0
getRegion OBJECT 獲取當(dāng)前地圖的視野范圍 1.4.0
getScale OBJECT 獲取當(dāng)前地圖的縮放級(jí)別 1.4.0

getCenterLocation 的 OBJECT 參數(shù)列表

參數(shù) 類型 必填 說(shuō)明
success Function 接口調(diào)用成功的回調(diào)函數(shù) ,res = { longitude: "經(jīng)度", latitude: "緯度"}
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

translateMarker 的 OBJECT 參數(shù)列表

參數(shù) 類型 必填 說(shuō)明
markerId Number 指定marker
destination Object 指定marker移動(dòng)到的目標(biāo)點(diǎn)
autoRotate Boolean 移動(dòng)過(guò)程中是否自動(dòng)旋轉(zhuǎn)marker
rotate Number marker的旋轉(zhuǎn)角度
duration Number 動(dòng)畫(huà)持續(xù)時(shí)長(zhǎng),默認(rèn)值1000ms,平移與旋轉(zhuǎn)分別計(jì)算
animationEnd Function 動(dòng)畫(huà)結(jié)束回調(diào)函數(shù)
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)

includePoints 的 OBJECT 參數(shù)列表

參數(shù) 類型 必填 說(shuō)明
points Array 要顯示在可視區(qū)域內(nèi)的坐標(biāo)點(diǎn)列表,[{latitude, longitude}]
padding Array 坐標(biāo)點(diǎn)形成的矩形邊緣到地圖邊緣的距離,單位像素。格式為[上,右,下,左],安卓上只能識(shí)別數(shù)組第一項(xiàng),上下左右的padding一致。開(kāi)發(fā)者工具暫不支持padding參數(shù)。

getRegion 的 OBJECT 參數(shù)列表

參數(shù) 類型 必填 說(shuō)明
success Function 接口調(diào)用成功的回調(diào)函數(shù),res = {southwest, northeast},西南角與東北角的經(jīng)緯度
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

getScale 的 OBJECT 參數(shù)列表

參數(shù) 類型 必填 說(shuō)明
success Function 接口調(diào)用成功的回調(diào)函數(shù),res = {scale}
fail Function 接口調(diào)用失敗的回調(diào)函數(shù)
complete Function 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

 

示例代碼:

 

<!-- 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,
      }]
    })
  }
})

 

更多微信小程序開(kāi)發(fā)教程,可以關(guān)注hi小程序。
重磅推薦:小程序開(kāi)店目錄

第一部分:小商店是什么

第二部分:如何開(kāi)通一個(gè)小商店

第三部分:如何登錄小商店

第四部分:開(kāi)店任務(wù)常見(jiàn)問(wèn)題

第五部分:小商店可以賣什么

第六部分:HiShop小程序特色功能

第七部分:小程序直播

第八部分:小程序收貨/物流

第九部分:小程序怎么結(jié)算

第十部分:小程序客服

第十一部分:電商創(chuàng)業(yè)

第十二部分:小程序游戲開(kāi)發(fā)