美文网首页
uni-app跳转地图

uni-app跳转地图

作者: 秋池_70b5 | 来源:发表于2020-04-09 17:20 被阅读0次

先写一份HTML代码。定义经纬度和点击事件

<template>

<view>

  <view @click="onGuideTap" data-latitude='35.41408' data-longitude='114.066224' data-bankName='新乡医学院'  style="margin-top:30rpx">

<view><text>地图导航</text></view>

    </view>

</view>

</template>

接下来呢,我们获取当前的地理位置和坐标。

onLoad: function(options) {

    this.setData({aww:app.globalData.userInfo})

    var that = this

    //获取当前的地理位置、速度

    wx.getLocation({

      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标

      success: function (res) {

        //赋值经纬度

        that.setData({

          latitude: res.latitude,

          longitude: res.longitude,

        })

      }

    })

  },

在这里我们使用setData将经纬度发到视图层。然后使用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

      })

    },

相关文章