溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

微信小程序如何創(chuàng)建并返回map上下文mapContext對(duì)象

發(fā)布時(shí)間:2022-03-10 14:44:01 來(lái)源:億速云 閱讀:917 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了微信小程序如何創(chuàng)建并返回map上下文mapContext對(duì)象的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇微信小程序如何創(chuàng)建并返回map上下文mapContext對(duì)象文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

wx.createMapContext(mapId)


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

關(guān)于“微信小程序如何創(chuàng)建并返回map上下文mapContext對(duì)象”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“微信小程序如何創(chuàng)建并返回map上下文mapContext對(duì)象”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI