W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
創(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)畫 | 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)畫持續(xù)時(shí)長(zhǎng),默認(rèn)值1000ms,平移與旋轉(zhuǎn)分別計(jì)算 |
animationEnd | Function | 否 | 動(dòng)畫結(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一致。開發(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,
}]
})
}
})
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: