微信小程序官方文档
https://mp.weixin.qq.com/debug/wxadoc/dev/api/location.html
JS代码
//index.js //获取应用实例 var app = getApp() Page({ data: { motto: ‘示例小程序-获取当前地理位、速度‘, userInfo: {}, hasLocation:false, location:{} }, //事件处理函数 bindViewTap: function() { wx.navigateTo({ url: ‘../logs/logs‘ }) }, onLoad: function () { console.log(‘onLoad‘) var that = this //调用应用实例的方法获取全局数据 app.getUserInfo(function(userInfo){ //更新数据 that.setData({ userInfo:userInfo }) }), wx.getLocation( { success: function( res ) { console.log( res ) that.setData( { hasLocation: true, location: { longitude: res.longitude, latitude: res.latitude } }) } }) } })
index.wxml
<!--index.wxml--> <view class="container"> <view bindtap="bindViewTap" class="userinfo"> <image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image> <text class="userinfo-nickname">{{userInfo.nickName}}</text> </view> <view class="usermotto"> <!-- <text class="user-motto">{{motto}}\n</text>--> <text>经度:{{location.longitude}}\n</text> <text>纬度:{{location.latitude}}</text> </view> </view>