微信小程序获取经纬度

1、在index.wxml文件创建如下代码,longitude,latitude为以获取经纬度为地图中心,获取当前位置经纬度信息

<view>纬度:{{latitude}}</view>
<view>经度:{{longitude}}</view>
<button bindtap="mapViewTap" style="margin:10px">查看地图</button>
<button bindtap="chooseMapViewTap" style="margin:10px">选择位置</button>

2、index.js文件如下,onload 事件自动获取经纬度位置并且显示到页面,点击查看地图通过 wx.getLocation为获取用户位置API,也可以点击选择位置按钮选择自己当前位置

Page({
  data: {
    latitude: '',
    longitude: ''
  },
  onLoad: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
      }
    })
  }
  ,// 查看地图
  mapViewTap: function () {
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        console.log(res)
        wx.openLocation({
          latitude: res.latitude,
          longitude: res.longitude,
          scale: 28
        })
      }
    })
  },
  // 选择位置
  chooseMapViewTap: function () {
    var that = this
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          location: {
            latitude: res.latitude,
            longitude: res.longitude,
            name: res.name
          }
        })
      },

    })
  },
})

具体显示如下图:

 

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值