本文編輯: JeremyLu瀏覽 4604
版權所有,嚴禁轉載
wx.createMapContext(mapId)
創建并返回 map 上下文 mapContext 對象,通過 mapId 跟一個 組件綁定,通過它可以操作對應的 組件。
方法 | 說明 |
---|---|
getCenterLocation | 獲取當前地圖中心的經緯度,返回的是 gcj02 坐標系,可以用于 wx.openLocation |
moveToLocation | 將地圖中心移動到當前定位點,需要配合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">獲取地圖中心點</button>
<text>中心點經度:{{longitude}}</text>
<text>中心點緯度:{{latitude}}</text>
<button bindtap="setCenterPoint">移動到當前位置</button>
</view>
js
//index.js
//獲取應用實例
var app = getApp()
Page({
data: {
latitude: 30.4919168,
longitude: 114.3061654
},
onReady: function(e){
//關聯<map />組件
this.mapCtx = wx.createMapContext('map')
},
//獲取地圖中心點
getCenterPoint: function(e){
var $this = this;
this.mapCtx.getCenterLocation({
success: function(res){
$this.setData({
latitude: res.latitude,
longitude: res.longitude
})
}
})
},
//將地圖中心點移動到當前位置
setCenterPoint: function(e){
this.mapCtx.moveToLocation()
}
})