欢迎光临,了解微信小程序开发,就上易用通!

微信小程序实例-获取当前的地理位置

发布:2018-01-30 11:42浏览: 来源:网络 作者:cola

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>
 

运行效果

微信小程序实例-获取当前的地理位置(图1)






免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。