本文編輯: JeremyLu瀏覽 4464
版權(quán)所有,嚴(yán)禁轉(zhuǎn)載
wx.createMapContext(mapId)
創(chuàng)建并返回 map 上下文 mapContext 對象,通過 mapId 跟一個(gè) 組件綁定,通過它可以操作對應(yīng)的 組件。
方法 | 說明 |
---|---|
getCenterLocation | 獲取當(dāng)前地圖中心的經(jīng)緯度,返回的是 gcj02 坐標(biāo)系,可以用于 wx.openLocation |
moveToLocation | 將地圖中心移動(dòng)到當(dāng)前定位點(diǎn),需要配合map組件的show-location使用 |
wxml
<view class="container">
<map id="map" longitude="{{longitude}}" latitude="{{latitude}}" show-location scale="12" style="width: 100%; height: 300px;"></map>
<button bindtap="getCenterPoint">獲取地圖中心點(diǎn)</button>
<text>中心點(diǎn)經(jīng)度:{{longitude}}</text>
<text>中心點(diǎn)緯度:{{latitude}}</text>
<button bindtap="setCenterPoint">移動(dòng)到當(dāng)前位置</button>
</view>
js
//index.js
//獲取應(yīng)用實(shí)例
var app = getApp()
Page({
data: {
latitude: 30.4919168,
longitude: 114.3061654
},
onReady: function(e){
//關(guān)聯(lián)<map />組件
this.mapCtx = wx.createMapContext('map')
},
//獲取地圖中心點(diǎn)
getCenterPoint: function(e){
var $this = this;
this.mapCtx.getCenterLocation({
success: function(res){
$this.setData({
latitude: res.latitude,
longitude: res.longitude
})
}
})
},
//將地圖中心點(diǎn)移動(dòng)到當(dāng)前位置
setCenterPoint: function(e){
this.mapCtx.moveToLocation()
}
})