先寫一份HTML代碼讯榕。定義經(jīng)緯度和點(diǎn)擊事件
<template>
<view>
? <view @click="onGuideTap" data-latitude='35.41408' data-longitude='114.066224' data-bankName='新鄉(xiāng)醫(yī)學(xué)院'? style="margin-top:30rpx">
<view><text>地圖導(dǎo)航</text></view>
? ? </view>
</view>
</template>
接下來呢拯爽,我們獲取當(dāng)前的地理位置和坐標(biāo)。
onLoad: function(options) {
? ? this.setData({aww:app.globalData.userInfo})
? ? var that = this
? ? //獲取當(dāng)前的地理位置渊跋、速度
? ? wx.getLocation({
? ? ? type: 'wgs84', // 默認(rèn)為 wgs84 返回 gps 坐標(biāo)血公,gcj02 返回可用于 wx.openLocation 的坐標(biāo)
? ? ? success: function (res) {
? ? ? ? //賦值經(jīng)緯度
? ? ? ? that.setData({
? ? ? ? ? latitude: res.latitude,
? ? ? ? ? longitude: res.longitude,
? ? ? ? })
? ? ? }
? ? })
? },
在這里我們使用setData將經(jīng)緯度發(fā)到視圖層。然后使用wx.openLocation占锯,打開地圖
methods: {
bindcontroltap(e) {
? ? var that = this;
? ? if (e.controlId == 1) {
? ? ? that.setData({
? ? ? ? latitude: this.data.latitude,
? ? ? ? longitude: this.data.longitude,
? ? ? ? scale: 14,
? ? ? })
? ? }
? },
? onGuideTap: function (event) {
? ? ? var lat = Number(event.currentTarget.dataset.latitude);
? ? ? var lon = Number(event.currentTarget.dataset.longitude);
? ? ? var bankName = event.currentTarget.dataset.bankname;
? ? ? console.log(lat);
? ? ? console.log(lon);
? ? ? wx.openLocation({
? ? ? ? type: 'gcj02',
? ? ? ? latitude: lat,
? ? ? ? longitude: lon,
? ? ? ? name: bankName,
? ? ? ? scale: 28
? ? ? })
? ? },