微信小程序中获取经纬度

来源:互联网 发布:年鹏外设淘宝店 编辑:程序博客网 时间:2024/06/05 07:48

index.wxml文件如下,longitude,latitude为以获取经纬度为地图中心,show-location为显示当前位置的绿色箭头

 <map  id="map" longitude="{{jd}}" latitude="{{wd}}"  style="width: 100%; height: 800rpx;"  show-location></map><view><text>经度{{jd}}</text></view><view><text>纬度{{wd}}</text></view>


index.js文件如下,wx.getLocation为获取用户位置API,首次需要用户授权,并打开gps,wgs84坐标系为gps坐标,gcj02为国测加密坐标,打开地图时使用。

onReady: function () {    var that = this    wx.getLocation({      type: 'wgs84',      success: function (res) {        console.log(res)        var latitude = res.latitude        var longitude = res.longitude        that.setData({          wd: latitude,          jd: longitude        })      }    })  },