微信小程序使用腾讯地图

第一步:设置起始跳转页

在wxml里面设置view控件

<view bindtap="toMap" data-address="{{address}}">
  <text>跳转到地图</text>
</view>

js里面设置跳转的值

Page({
  data: {
    address:"成都市"
  },

  onLoad: function (options) {

  },
  toMap(e) {
    wx.navigateTo({
      url: '/pages/map/map?address=' + e.currentTarget.dataset.address,
    })
  }
})

第二步:跳转后对值进行处理

Page({
  data: {
    latitude: "",
    longitude: "",
    markers: [{
      id: 1,
      latitude: "",
      longitude: "",
    }]
  },
  onLoad: function(options) {
    var address = options.address;
    console.log(address);
    this.getLocation(address);
  },
  getLocation(address) {
    var that = this;
    wx.request({
      url: 'https://apis.map.qq.com/ws/geocoder/v1/',
      method: 'get',
      data: {
        key: "",       //这里的key要在腾讯地图上面申请
        address: address
      },
      success(res) {
        console.log(res);
        that.setData({
          latitude: res.data.result.location.lat,
          longitude: res.data.result.location.lng,
          "markers[0]": {
            latitude: res.data.result.location.lat,
            longitude: res.data.result.location.lng
          }
        })
      },
      fail(err) {
        console.log(err);
      }
    })
  }
})

设置map控件的高度,长度以及值(markers是设置显示自己的位置)

效果图

点击跳转到地图
在这里插入图片描述
跳转成功
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值